chevron_left
399 points
8 4 3

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 forms 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 modifier.

 

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 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.


 

1 Comment

This is what I was lookingfor. I always enjoy your answers which are of great help. Love from Pakistan

More Posts

what is the difference between encapsulation and abstraction in java kesav.eee - Jun 1
Difference between Abstraction and Encapsulation in C# with example mhfaisal - May 7
Difference between abstraction and encapsulation in c# with a real-time example. Farkhanda Athar - Sep 1, 2020
What is the difference between id and class in css Sanjana Sagar - May 22
What is the difference between ascii and unicode Sanjana Sagar - May 28
What is the difference between thread and process Sanjana Sagar - May 28
What is the difference between a stack and a queue Sanjana Sagar - May 28
What is the difference between varchar and nvarchar Sanjana Sagar - May 22
What is the difference between pre and post Sanjana Sagar - May 20
What is the difference between oltp and olap Sanjana Sagar - May 20