Abstraction
vs Encapsulation – Java OOPS
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 basis of Object oriented programming in Java.
Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming in Java
Java - Object Oriented Programming [For Absolute Beginners]
Abstraction and Encapsulation in Java are two important Object
oriented programming concept and they are completely different to each
other. Only similarity between Abstraction and Encapsulation is that they are
OOPS concept, other than that they mean two different things. Abstraction represent taking out the behavior from How exactly its implemented, one example
of abstraction
in Java is interface while Encapsulation means hiding details of
implementation from outside world so that when things change no body 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 were
public all your clients would have been affected. Apart from this main
difference in behavior here are couple of more differences between Abstraction and Encapsulation in Java.
Abstraction vs Encapsulation in Java

1) First difference between Abstraction and Encapsulation is that,
Abstraction is implemented in Java using 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.
That's all from my side on differences between Abstraction and
Encapsulation in Java. Correct understanding of Encapsulation and Abstraction
is 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
concept.
Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming in Java
Java - Object Oriented Programming [For Absolute Beginners]
Other Java programming and OOPS tutorials
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