What is Encapsulation?
Encapsulation means data protection.
In Java, encapsulation is the concept of wrapping data (variables) and methods together and controlling who can access them.
π In simple words:
Encapsulation = Protecting data from unauthorized access
How is Encapsulation Achieved in Java?
Encapsulation is achieved using Access Modifiers.
Access modifiers decide:
- Who can access a class
- Who can access variables
- Who can access methods
- Who can access constructors
What are Access Modifiers?
Access modifiers define the visibility of class members.
Java has 4 access modifiers:
private-
default(no keyword) protectedpublic
Example
Parent Class: Nanthakumar
public class Nanthakumar {
private int salary = 10000;
String task = "Dev";
protected String headPhone = "redmi";
public String designation = "Development";
}
Here we have declared variables with different access modifiers.
Main Class: Karthick
public class Karthick {
public static void main(String[] args) {
Karthick k = new Karthick();
// System.out.println(k.salary); // Not allowed
Nanthakumar n = new Nanthakumar();
System.out.println(n.task);
System.out.println(n.headPhone);
System.out.println(n.designation);
}
}
Child Class: Ruby
public class Ruby extends Nanthakumar {
public static void main(String[] args) {
Nanthakumar n = new Nanthakumar();
// System.out.println(n.task); // Not allowed
Ruby r = new Ruby();
System.out.println(r.headPhone);
System.out.println(r.designation);
}
}
Understanding Each Access Modifier
1οΈβ£ Private
private int salary = 10000;
- Accessible only inside the same class
- Cannot be accessed outside the class
- Used for maximum data protection
β Not accessible in Karthick
β Not accessible in Ruby
π This shows strong encapsulation
2οΈβ£ Default (No Keyword)
String task = "Dev";
- Accessible within the same package
- Not accessible outside the package
- Also called package-private
β Accessible in Karthick (same package)
β Not accessible in Ruby (different class via inheritance)
3οΈβ£ Protected
protected String headPhone = "redmi";
- Accessible within the same package
- Accessible in child class even outside the package
β Accessible in Karthick
β Accessible in Ruby (because it extends Nanthakumar)
4οΈβ£ Public
public String designation = "Development";
- Accessible from anywhere
- No restriction
β Accessible in Karthick
β Accessible in Ruby
Access Modifier Visibility Table
| Modifier | Same Class | Same Package | Subclass | Anywhere |
|---|---|---|---|---|
| private | β | β | β | β |
| default | β | β | β | β |
| protected | β | β | β | β |
| public | β | β | β | β |
Why Encapsulation is Important?
- Protects sensitive data
- Prevents unauthorized access
- Improves code security
- Makes code easy to maintain
- Follows real-world object behavior
Real-Life Example
Think of an ATM card:
- PIN β
private - Bank branch access β
protected - Customer details β
default - Bank name β
public
You cannot access everything directly β this is encapsulation.
Key Points (For Revision)
- Encapsulation means data protection
- Achieved using access modifiers
-
privateβ most secure -
publicβ accessible everywhere -
protectedβ inheritance-friendly -
defaultβ package-level access
Top comments (0)