
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Abstraction vs Encapsulation in Java
Encapsulation
Encapsulation is one of the four fundamental OOPs concepts. The other three are inheritance, polymorphism, and abstraction.
Encapsulation in Java is a mechanism for wrapping the data (variables) and the code acting on the data (methods) together as a single unit. In encapsulation, the variables and methods within a class are hidden using the private access specifier (though it's not mandatory for all variables and methods to be private). This is known as data hiding.
To access private variables and methods from other classes, one of the simplest ways is by using getter and setter methods.

Fig 1: To showcase encapsulation in the form of a capsule
For example, in the image given above, we have a capsule that represents a class. Inside that, the blue balls are variables, and the green balls are methods, both are enclosed in the capsule to show the concept of encapsulation.
One of the ways to demonstrate encapsulation in Java is to -
- Declare the variables of a class as private.
- Provide public setter and getter methods to modify and retrieve the variable's values, respectively.
Example to demonstrate encapsulation
Below is an example to demonstrate encapsulation in Java by using a private variable and a public getter method -
class Encap { private int x = 5; public int getX() { return x; } public void setX(int x) { this.x = x; } } public class Main { public static void main(String[] args) { Encap e = new Encap(); System.out.println(e.getX()); } }
Following is the output of the above code -
5
Abstraction
Abstraction is a process of hiding the implementation details, providing only the necessary functionalities to the user.
For example, when you consider the case of e-mail, complex details such as what happens as soon as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore, to send an e-mail, you just need to type the content, mention the address of the receiver, and click send.

In other words, the user will have the information on what the object (through functions) does instead of how it does it. In Java, abstraction is achieved using Abstract classes and interfaces.
Example to demonstrate abstraction
Below is an example to show abstraction in Java using an abstract class and method:
abstract class Animal { abstract void sound(); } class Dog extends Animal { void sound() { System.out.println("Bark"); } } public class Main { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } }
Following is the output of the above code -
Bark
Difference Between Abstraction and Encapsulation
Following are the differences between abstraction and encapsulation in Java:
Abstraction | Encapsulation |
The abstraction is implemented in Java by using the abstract class and interface. | The encapsulation is implemented by using private-package, private, and protected access modifiers. |
The abstraction allows only selective details while hiding the complexity. | Encapsulation restricts direct access to data by hiding it within the class. |
To perform the abstraction, the data should be encapsulated. | We can do encapsulation without necessarily needing the abstraction. |
Abstraction focuses on "What" the object does. | Encapsulation focuses on "How" the object does it. |