What is Abstraction in Java - the WHY and the Truth
- 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:
- Understand Classes and Objects in Java
- Understand encapsulation in Java
- Understand inheritance in Java
- Understand polymorphism in Java
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Java Variables: Passed by Value or Reference?
- Understand Java access modifiers
- Understand Java package and import statements
Comments
I recommend you to read the articles mentioned in the Related Tutorials at the bottom of this article.
I am always looking for documentation on this matter.
can you share with me any other document or source.