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:-
Here is a nice diagram that explains the concept of Abstraction in object-oriented programming:-
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
- My favorite courses to learn object-oriented programming
- 5 differences between Hashtable and HashMap in Java
- 5 Free Object-Oriented programming courses for beginners
- 10 Java Coding Interview Questions and Answers for Java beginners.
- Difference between HashMap and ConcurrentHashMap in Java
- 7 best OOP design pattern courses for Java developers
- What is a factory method design pattern in Java
- Difference between TreeSet and HashSet in Java
- Top 5 Courses to Learn Design Patterns in Java
- 5 Free Data Structure and Algorithms Courses
- 5 Free Spring Framework Courses for Java Developers
- 5 Free Linux Courses for Programmers and Developers
- 5 Free Courses to Learn React JS Framework
- Top 5 Courses to Learn TensorFlow and Machine Learning
- Top 5 Courses to learn Web Development in 2021
- 10 Free Courses to learn Java for Beginners and Experienced Programmers
Thanks for reading this article so far. If you find these OOP Analysis and Design courses useful then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this Object Oriented Programming (OOPs) for the JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
This is nonsense. Your definition of abstraction and encapsulation are the same.
ReplyDeleteHow 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"Encapsulation is implemented using private keyword and other access modifier."
DeleteAbove 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.
need more clear concept about abstraction...
ReplyDeleteexamples explaining both concepts would be helpful.
ReplyDeleteWell 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.
ReplyDeleteThis is correct. Encapsulation is actually a kind of abstraction only.
DeleteWell Abstraction is the concept and encapsulation is mechanism to fulfil that concept :)
ReplyDeletewell said sir...
Deleteencapsulation is not a mechanism but a principle or conecpt like abstraction
DeleteI think following link
ReplyDeletehttp://lavraham.com/node/21 defines what pradip says more clearly.
but how they are complementary to each other????
ReplyDeleteAbstraction 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)
ReplyDeleteEncapsulation 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.
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.
ReplyDeleteOn 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.
Good Example of encapsulation is POJO
ReplyDeleteVery good questions . Here are some 30 Java OOP concept interview questions, I have found immensely useful. Thanks
ReplyDeleteAbstraction is hiding the information or providing only necessary details to the client.
ReplyDeletee.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)
You just rock man
ReplyDeletegreat explaination anoop thanks
ReplyDeleteI'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.
ReplyDeletehello Ravi, very interesting observation. Yes, I would rather say, Abstraction simplifies design and Encapsulation hides implementation.
Delete