Difference between Abstraction and Encapsulation in Java and Object Oriented Programming? [Answer] (original) (raw)

The difference between Abstraction and Encapsulation in Java is a common question in Java interviews for Beginners to check their understanding of Object-Oriented Programming and their ability to use them. Abstraction and Encapsulation in Java are two important Object-oriented programming concepts and they are completely different from each other. The only similarity between Abstraction and Encapsulation is that they are_OOP concepts_, other than that they mean two different things, but there is a confusing thing, both hide details to simplify things but for different reasons.

Abstraction represent taking out the behavior from how exactly is implemented. For example, instead of describing the engine as a diesel engine or steam engine, you just say engine. One example of abstraction in Java is an interface while Encapsulation means hiding details of implementation from the outside world so that when things change nobody gets affected.

One example of Encapsulation in Java is private methods; clients don't care about it, You can change, amend or even remove that method if that method is not encapsulated and it was public all your clients would have been affected.

Apart from this main difference in behavior, I mean Abstraction hides details for simplification while Encapsulation hides details for making it easier to change, we will see a couple of more differences between Abstraction and Encapsulation in Java.

By the way, if you are new to Java and the Object-Oriented world then I highly recommend you to join one of these best Object Oriented Programming in Java course like The Complete Java Masterclass by Tim Buchalaka on Udemy. This 80-hour long course is the best course to learn OOP with Java. The article also contains a couple of more choices depending upon your experience level and learning style.

Abstraction vs Encapsulation in Java

Here are some of the main differences between Abstraction vs Encapsulation in Java and OOPS(Object Oriented programming) concept. Abstraction and Encapsulation along with Inheritanceand polymorphismform the basis of Object-oriented programming in Java.

  1. The first difference between Abstraction and Encapsulation is that Abstraction is implemented in Java using the interfaceand abstract class while Encapsulation is implemented using private, package-private, and protected access modifiers.

  2. Encapsulation is also called data hiding.

  3. Design principles "programming for interface than implementation" is based on abstraction and "encapsulate whatever changes" is based upon Encapsulation.

Here is a nice diagram that explains the concept of Abstraction in object-oriented programming:-

Difference between Abstraction and Encapsulation in Java - OOP

In this diagram, you can see that the user only knows there is a book window for borrowing and returning books, he doesn't care or doesn't need to know about how the book is found and what happens before and after he requests or return a book.

In other words, he is abstracted from all the details involved in getting the book to the window.

Another common example of Abstraction is eating your lunch or dinner. What you care about is eating, you don't need to bother about how the raw materials are produced, cooked, and served, all those things are abstracted from you.

That's all from my side on the differences between Abstraction and Encapsulation in Java. Correct understanding of Encapsulation and Abstraction is a must for any Java developer. Head First Object-oriented Analysis and design is a great book to learn more about Abstraction, Encapsulation, and other OOPS concepts.

Other Java programming and OOP tutorials You may like