Encapsulation Vs Abstraction
Hiding !
🔆 Encapsulation is represented by data hiding from direct access, which means information hiding.
It makes variables private and accesses them using public getter and setter methods.
public class Bank {
private double balance = 0;
public double getBalance() {
return balance;
}
public double setBalance() {
this.balance=balance;
}
}What is happening ?
- The balance variable is private.
- We cannot access them directly.
- This protects data.
🔆 In contrast, Abstraction is used to detail hiding, which means implementation hiding.
Get Kaveesha Senarathne’s stories in your inbox
Join Medium for free to get updates from this writer.
Achieved using abstract classes or interfaces.
abstract class Animal {
abstract void sound ();
}
class Dog extends Animal {
void sound () {
System.out.println (“Bark….”);
}
}
class Cat extends Animal {
void sound () {
System.out.println (“Meow….”);
}
}What is happening ?
- The Animal class hides the implementation of sound().
- We only know that animals make sounds.
- How the dog makes sound is hidden inside the Dog class, and cat makes sound is hidden inside the Cat class.
- This is Implementation Hiding.
Furthermore, Encapsulation focuses on data and methods staying together, but abstraction focuses on the interface, not implementation.









