What is the difference between Encapsulation and Abstraction in Java?

  • 4.6/5
  • 52
  • May 03, 2025
The terms Encapsulation and Abstraction are closely related concepts in Java and Object-Oriented Programming (OOP), but they serve different purposes and are implemented in distinct ways. Here's a detailed breakdown of the differences between them:

- Encapsulation is about protecting data and controlling access to it, making sure that internal details are hidden from the outside.
- Abstraction is about providing a simple interface while hiding the complexity of how the system or object works.

Real-World Analogy
Encapsulation: Imagine a pill capsule that hides the medicine inside. You don’t need to know what’s inside, but you can control how much of it is dispensed (via getters/setters).

Abstraction: Imagine using a TV remote. You don’t need to know how the internal circuits work (implementation), you just need to know the buttons (interface) and their functions (what it does).

Example

- Encapsulation: The balance is private and can only be accessed or modified via the public methods getBalance() and deposit().
- Abstraction: The Vehicle class provides an abstract method startEngine(), hiding the specific engine-starting logic inside its subclass (Car).

Summary
Aspect Encapsulation Abstraction
What is Hidden? Internal data (variables). Implementation details (methods).
Goal Protect and control data access. Hide complexity and expose behavior.
How It’s Achieved Using access modifiers and getter/setter methods. Using abstract classes or interfaces.
Example Private variables, public getters/setters. Abstract class or interface with implemented methods.
Index
Why is Java a platform independent language?

2 min

Is Java a pure object oriented language?

2 min

What is the difference between Heap and Stack memory in Java, and how does Java utilize them?

2 min

What do you mean by data encapsulation in Java?

2 min

What Do You Mean by Abstraction in Java?

2 min

What is the difference between Encapsulation and Abstraction in Java?

3 min

What is a JIT Compiler in Java?

2 min