Difference between Abstraction and Encapsulation in Java and Object Oriented Programming? [Answer]

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 Inheritance and polymorphism form 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 interface and 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

21 comments:

  1. This is nonsense. Your definition of abstraction and encapsulation are the same.

    ReplyDelete
    Replies
    1. How come definition is same, Its clearly saying that abstraction is implemented using interface and abstract class and Encapsulation is implemented using private keyword and other access modifier.

      Delete
    2. "Encapsulation is implemented using private keyword and other access modifier."

      Above statement is wrong.


      Encapsulation achieve by encapsulating the data and functionality in the same place , either it on the class or in the same method. it is not related to the access modifiers.

      Delete
  2. need more clear concept about abstraction...

    ReplyDelete
  3. examples explaining both concepts would be helpful.

    ReplyDelete
  4. Well Abstraction and encapsulation are both viewing same thing in java with different point. here abstarction means showing functionality hiding information i.e complexity and in encapsulation u do same thing u wrap up things within a class which will encapsulates. wat i can say is abstraction is evalved from encapsulation by its implementation and abstraction by its use. i.e encapsulation is part of abstraction. Ex: creating a class is encapsulation and way of implementation i.e by using intereface and abstract classes is abstraction.

    ReplyDelete
    Replies
    1. This is correct. Encapsulation is actually a kind of abstraction only.

      Delete
  5. Well Abstraction is the concept and encapsulation is mechanism to fulfil that concept :)

    ReplyDelete
    Replies
    1. well said sir...

      Delete
    2. encapsulation is not a mechanism but a principle or conecpt like abstraction

      Delete
  6. I think following link
    http://lavraham.com/node/21 defines what pradip says more clearly.

    ReplyDelete
  7. but how they are complementary to each other????

    ReplyDelete
  8. Abstraction means that you've just named the behaviors and outline of your class, and implementations are there to be written somewhere else in detail (not mattering using which way/strategy)

    Encapsulation means that you implement your whole class (not mattering whether you've made an abstract before or not) and you put some restrictions in this very class so that no one can access it or change it directly.

    ReplyDelete
  9. Abstraction is a way to talk in general terms, instead of talking about playing cricket, you talk about playing. Instead of talking nitty gritty about how to sort an array using quicksort, you just talk about sorting. The higher the level of abstraction, the more general it becomes. This is why right level of abstraction is needed between components. It become increasingly important while inter acting between components. This is where abstract class and interface comes into picture, they are Java's tool to assist a program with abstraction. interface is generally more abstract then their implementaiton and that's why if two component talk with interface, they are easy to change and extend.

    On the other hand encapsulation is about hiding internal details? Why? because it allows you to change the internals of an object without affecting extenal client. If an outside object knows how a car run, you cannot change it but if it just know that it runs but not how, you can improve how it runs. Java provides sevral access modifier e.g. public, private, protected and package to keep encapsulation in check.

    ReplyDelete
  10. Good Example of encapsulation is POJO

    ReplyDelete
  11. Very good questions . Here are some 30 Java OOP concept interview questions, I have found immensely useful. Thanks

    ReplyDelete
  12. Abstraction is hiding the information or providing only necessary details to the client.

    e.g Car Brakes- You just know that pressing the pedals will stop the vehicle but you dont need to know how it works internally.

    Advantage of Abstraction Tomorrow if brake implementaion changes from drum brake to disk brake, as a client, you dont need to change(i.e your code will not change)

    Encpasulation is binding the data and bahaviours together in a single unit. Also it is a language mechanism for restricting access to some components(this can be achived by access modifiers like private,protected etc.)

    For e.g. Class has attributes(i.e data) and bahaviours (i.e methods that operate on that data)

    ReplyDelete
  13. great explaination anoop thanks

    ReplyDelete
  14. I'm not agree with your explanation, actually abstraction is a hypothesis which expose the behaviour and hide all the complex implementation and encapsulation is a practicality to hide or encapsulate the chunk of data and functionality within a class/capsule to hide the complex implementation. So the main difference is Abstaraction is a hypothetical process and encapsulation is a practical process to hide user non relevant complex implementation.

    ReplyDelete
    Replies
    1. hello Ravi, very interesting observation. Yes, I would rather say, Abstraction simplifies design and Encapsulation hides implementation.

      Delete

Feel free to comment, ask questions if you have any doubt.