Abstraction deals with hiding the details and showing the essential things to the user whereas encapsulation bind your data and code togethe...
Abstraction deals with hiding the details and showing the essential things to the user whereas encapsulation bind your data and code together as a single unit, or in java you can say it takes all your methods, variables and bind them together in a single class.
Also, abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.
Both these OOPs concepts perform differently to reduce the complexity of the overall program.
Now, let’s understand these two concepts in detail.
Abstraction: It is basically the quality of dealing with ideas rather than events. Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users. Let’s see this real-life example of a car where i’ll help you understand what exactly abstraction is.
If you consider the case of this car, here the mechanic is repairing a certain function in a car. But the user or you can say driver doesn’t want to know about these things, he just wants his car back in a working condition. So here, you basically segregate the implementation and show the other person what he actually wants to see and that exactly refers to abstraction.
Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit. Refer to the below image where all your methods, variables are binded together in a single class.
In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class.
COMMENTS