Wednesday, October 20, 2021

Difference between Abstraction and Encapsulation in Java? OOP Question Answer

Both Abstraction and Encapsulation are two of the four basic OOP concepts which allow you to model real-world things into objects so that you can implement them in your program and code. Many beginners get confused between Abstraction and Encapsulation because they both look very similar. If you ask someone what is Abstraction, he will tell that it's an OOP concept which focuses on relevant information by hiding unnecessary detail, and when you ask about Encapsulation, many will tell that it's another OOP concept which hides data from the outside world. The definitions are not wrong as both Abstraction and Encapsulation do hide something, but the key difference is on intent.

Abstraction hides complexity by giving you a more abstract picture, a sort of 10,000 feet view, while Encapsulation hides internal working so that you can change it later. In other words, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.

For example, when you first describe an object, you talk in more abstract terms e.g. a Vehicle that can move, you don't tell how the Vehicle will move, whether it will move by using tires or it will fly or it will sell. It just moves. This is called Abstraction. We are talking about the most essential thing, which is moving, rather than focusing on details like moving in-plane, sky, or water.

There are also different levels of Abstraction and it's good practice that classes should interact with other classes with the same level of abstraction or higher level of abstraction. As you increase the level of Abstraction, things start getting simpler and simpler because you leave out details.


On the other hand, Encapsulation is all about implementation. Its sole purpose is to hide the internal working of objects from the outside world so that you can change it later without impacting outside clients.

For example, we have a HashMap which allows you to store the object using the put() method and retrieve the object using the get() method. How HashMap implements this method (see here) is an internal detail of HashMap, the client only cares that put stores the object and get return it back, they are not concerned whether HashMap is using an array, how it is resolving the collision, whether it is using linked list or binary tree to store object landing on the same bucket, etc.

Because of Encapsulation, you can change the internal implementation of HashMap with ease without impacting clients who are using HashMap. For example, in Java 8, the java.util.HashMap changes its implementation to use a binary tree instead of LinkedList to store objects in the same bucket after a certain threshold (see here).

The client doesn't need to make any change to benefit from this code change because those details are not exposed to them. Had the client known about that i.e. somehow they can get the reference of the internal array from HashMap, it would not have been possible to change the implementation without impacting clients.

Btw, if you are new to the world of object-oriented programming and design then I also suggest you go through a hands-on course like UML and the Object-Oriented Design Foundations course by Karloy Neisztor on Udemy. It's a great course to understand the complete process of object-oriented analysis and design for creating quality software.



There are many design principles that are based on Abstraction e.g. "coding for interfaces then implementation" which helps you to write flexible code in Java or C++. The idea is that a class depends upon an interface, a higher level of abstraction than the class, a lower level of abstraction. This results in a flexible code that can work with any implementation of the interface.

For example, if you need HashMap, your class should depend upon Map instead of HashMap. Similarly, if you need ArrayList, make sure you should use the List

Thankfully, Uncle Bob has shared several such design principles on Clean Code, collectively known as SOLID design principles, which is something every OOP programmer must learn and understand.

Difference between Abstraction and Encapsulation in Java - OOP



Difference between Abstraction vs Encapsulation

Here are some of the key differences between Encapsulation and Abstraction in point format for your quick revision:

1) The most important difference between Abstraction and Encapsulation is that Abstraction solves the problem at the design level while Encapsulation solves its implementation level.

2) Abstraction is about hiding unwanted details while giving out the most essential details, while Encapsulation means hiding the code and data into a single unit e.g. class or method to protect the inner working of an object from the outside world. In other words, Abstraction means extracting common details or generalizing things.

3) Abstraction lets you focus on what the object does instead of how it does, while Encapsulation means hiding the internal details of how an object works. When you keep internal working details private, you can change them later with a better method. The Head First Object-Oriented Analysis and Design book has some excellent examples of these OOP concepts, I suggest you read that book at least once to revisit OOP fundamentals.



4) Abstraction focus on outer lookout e.g. moving of vehicle while Encapsulation focuses on internal working or inner lookout e.g. how exactly the vehicle moves.

5) In Java, Abstraction is supported using the interface and abstract class while Encapsulation is supported using access modifiers e.g. public, private, and protected.


Here is a nice table highlighting key differences between Abstraction and Encapsulation in Object-Oriented Programming:

Difference between Abstraction and Encapsulation


That's all about the difference between Abstraction and Encapsulation in Java and OOP. I understand, they both seem very similar but as I said they are a totally different concept. Just remember that Abstraction solves the problem at the design level while Encapsulation solves at the implementation level. Both are very important for an OOP programmer but sometimes it can be difficult to explain.

As I have said before, the best way to learn and master Object-Oriented programming is by writing code and reading the code of others. The more you are exposed to code, the more you realize how these concepts work in practice. There are several design principles that are based in terms of Abstraction like coding for interface rather than implementation which allows you to write flexible code.


Other OOP concept tutorials you may like
  • 5 Books to Learn Object-Oriented Programming and Design (book)
  • What is the difference between Class and Object in Java or OOP? (answer)
  • The difference between Inheritance and Polymorphism in Java? (answer)
  • The difference between Aggregation, Composition, and Association in OOP? (answer)
  • The difference between State and Strategy design patterns? (answer)
  • What is Polymorphism in Java? Overloading or Overriding? (answer)
  • What is the difference between Overloading and Overriding in OOP? (answer)
  • The difference between instance and object in Java? (answer)
  • What is the difference between static and dynamic binding in Java? (answer)
Thanks for reading this article, if you really like this article then please share it with your friends and colleagues. If you have any questions or suggestions then please drop a comment. If you want to read more about OOP concepts, SOLID design principles, and applying OOP concepts in real life, just read Clean Code.

20 comments :

Jitendra said...

It have been explained nicely. Very good article.
Thanks.

javin paul said...

Thanks Jitendra, glad you liked my article. Please share with your friends and colleagues as well.

Gokul said...

Perfect explanation...can never mixed it again.

Rohit Dhingra said...

nice article

Unknown said...

very nice article and very well explained. searched for this difference at many places but this one cleared the concept.

Anonymous said...

Good explanation.

javin paul said...

Thank you @Anonymous, glad that you find these differences between abstraction and encapsulation useful.

Abhinav said...

Nice explanation...

Unknown said...

One of the best article to explain the subtle pratical difference between the two. Very well explained and understood.

javin paul said...

Thank you unknown, very happy that this article help you to understand difference between Abstraction and Encapsulation better.

Allea John said...

Abstraction is used to show only useful data where as Encapsulation is used to wrap the necessary information.

Unknown said...

Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation. Abstraction on the other hand means showing only the necessary details to the intended user.

Akshat Kumar Verma said...

Hi Javin ,
Thanks for sharing your thoughts and for your wonderful blog which I think everyone loves. Actually I have a doubt regarding the explanation given like what I feel the term Abstract is itself tell us that it the just gives idea of what the thing is...like abstract paintings ,which prepares a structure under which you prepare the building by your thoughts.
But encapsulation is as name suggests wrapping up all the ingredients into a same wrapper required to make a functionality completed.Just like medicine capsules.
I feel Both of them are different but works together. As we say in design language highly cohesive

javin paul said...

Absolutely correct Akshat, you provided even better explanation :-) keep it up

boomsl202 said...

Thank you so much )

Anonymous said...

thank god finally i could understand.

Rajeev said...

superb explanation

Anonymous said...

could you explain like this in YouTube also.I think it will be more effective.

javin paul said...

Hello @Anonymous, stay tuned, we'll start posting tutorials on Youtube also.

kashif ghafoor said...

excellent article. beautiful explanation

Post a Comment