Abstraction Vs Encapsulation

Java is an object-oriented programming language and it follows OOPs concepts. The OOPs concepts include classes, objects, polymorphism, inheritance. There are two other features of OOPs i.e. abstraction and encapsulation. They both seem very similar but totally different in concept and implementation. The major difference between abstraction and encapsulation is that abstraction hides the code complexity while encapsulation hides the internal working from the outside world. In this section, we will discuss abstraction and encapsulation and the differences between abstraction and encapsulation in Java.

Abstraction

It is a feature of OOPs. It is used to hide the unnecessary information or data from the user but shows the essential data that is useful for the user. It can be achieved by using the interface and the abstract class. In interfaces, only the methods are exposed to the end-user. The best example of abstraction is a TV remote. The user only interacts with the outer interface that is nothing but keys. The user only knows which key to press for what function.

Let's understand the abstraction through a Java program.

Encapsulation

It is also a feature of OOP. It is used to bind up the data into a single unit called class. It provides the mechanism which is known as data hiding. It is an important feature of OOPs. It prevents to access data members from the outside of the class. It is also necessary from the security point of view.

Let's understand the abstraction through a Java program.

EncapsulationDemo.java

Account.java

Difference Between Abstraction and Encapsulation

AbstractionEncapsulation
Abstraction is a feature of OOPs that hides the unnecessary detail but shows the essential information.Encapsulation is also a feature of OOPs. It hides the code and data into a single entity or unit so that the data can be protected from the outside world.
It solves an issue at the design level.Encapsulation solves an issue at implementation level.
It focuses on the external lookout.It focuses on internal working.
It can be implemented using abstract classes and interfaces.It can be implemented by using the access modifiers (private, public, protected).
It is the process of gaining information.It is the process of containing the information.
In abstraction, we use abstract classes and interfaces to hide the code complexities.We use the getters and setters methods to hide the data.
The objects are encapsulated that helps to perform abstraction.The object need not to abstract that result in encapsulation.