- Details
- Written by Nam Ha Minh
- Last Updated on 26 September 2019   |   Print Email
1. What is Abstraction?
Perhaps, abstraction is the most confusing concept in OOP. Most guys tend to think that abstractions relates to interfaces and abstract classes. But that’s not true. From my experience, a majority of programmers don’t have a fully and proper understanding about this matter.So today would be the day you truly understand abstraction in general and abstraction in Java.Abstraction is a general concept that denotes the progress of modeling “real things” into programming language. For example, when you write a class named
Dog, you abstract a real dog into a type (class) in Java:
class Dog {
}
When you declare a field named
breed in the
Dog class, you also abstract the real attribute of the dog in terms of a class’ variable:
class Dog {
String breed;
}
When you implement a method named
bark() in the
Dog class, you also abstract the real behavior, barking, of a dog in terms of a method in a class:
class Dog {
String breed;
void bark() {
System.out.println("Gow gow!");
}
}
As you can see, this
Dog class abstracts a real dog in the real world in terms of a type (class) with fields (abstract the characteristics) and methods (abstract the behaviors). Abstraction is showing the essential features of an object.When you declare an interface with empty methods, you also use abstraction:
interface Animal {
void eat();
void move();
void sleep();
}
When you declare a variable, you also use abstraction:
int numberOfLegs = 4;
From the examples above, everything is an abstraction. The progress of abstraction happens when you declare an interface, write a class, implement a method, declare a variable, etc. Everything is an abstraction.
2. Why Is Abstraction Important?
Suppose that abstraction is not there, we cannot identify an object via its class name.If abstraction is not there, we cannot access the attributes of an object.If abstraction is not there, we cannot invoke the behaviors of an object.Having said that, abstraction is the most fundamental concept on which others rely on, such as encapsulation, inheritance and polymorphism.
3. The Truth About Abstraction in Java
- Abstraction is not about interfaces or abstract classes. Abstraction is the progress of modeling real world objects into programming language. Hence interfaces and abstract classes are just two techniques used in this progress.
- In an Object-Oriented Programming language like Java, everything is an abstraction: interface, class, field, method, variable, etc.
- Abstraction is the fundamental concept on which other concepts rely on: encapsulation, inheritance and polymorphism.
Related Tutorials:
Other Recommended Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on
Facebook and watch
his Java videos you YouTube.