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.

Fig 2: The above figure showcases the abstraction

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.
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-07-14T18:41:12+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements