Difference Between Encapsulation and Abstraction in Java

By | September 23, 2018

Encapsulation and Abstraction are two fundamental concepts of object-oriented programming (OOP) in Java. While both of these concepts aim to improve the quality of code, they serve different purposes.

Encapsulation is the process of hiding the internal details of an object from the outside world and restricting direct access to its internal state. This is done by creating private instance variables and providing public getters and setters methods to access and modify them. The encapsulated class
controls access to its own data, and this helps to prevent unintended or unauthorized changes to the object’s state.

Abstraction is the process of hiding the complexity of an object and showing only the necessary details to the user. This is done by using abstract classes and interfaces. An abstract class is a class that cannot be instantiated and is meant to be subclassed by other classes. An interface is a
collection of abstract methods that define a set of behaviors but do not provide any implementation details. Abstraction allows for the creation of generic programming constructs that can be used with multiple objects, without needing to know the specific details of each object.

In summary, encapsulation is concerned with protecting an object’s internal state, while abstraction is concerned with hiding the complexity of an object’s behavior. Both of these concepts are important for creating well-designed, maintainable, and reusable code in Java.

You May Also Like:

What are JVM, JRE and JDK in Java?
Differences between Java EE and Java SE
Encapsulation in Java
Abstraction in Java
Understating of Polymorphism in Java
Method Overloading or Static Polymorphism in Java
Method Overriding or Dynamic Polymorphism in Java
Understating of Inheritance in Java

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *