Today I learned one of the most important Object-Oriented Programming (OOP) concepts in Java β Encapsulation.
Encapsulation is used to protect data and control access to variables and methods inside a class.
π¦ What is Encapsulation?
The word Encapsulation comes from the concept of a Capsule.
A capsule keeps medicines safely enclosed inside it.
Similarly in Java:
Encapsulation means wrapping data (variables) and code (methods) together in a single unit (class) and restricting direct access to some components.
π In simple words:
Encapsulation = Data Protection
π― Why Do We Use Encapsulation?
Encapsulation provides several advantages:
β Protects sensitive data
β Prevents unauthorized access
β Improves code maintainability
β Makes code more secure
β Allows controlled access using methods
π How Do We Protect Data in Java?
Java uses Access Modifiers to control access.
These modifiers define who can access variables, methods, and classes.
π Types of Access Modifiers in Java
Java provides four types of access modifiers:
| Access Modifier | Same Class | Same Package | Subclass | Different Package |
| --------------- | ---------- | ------------ | -------- | ----------------- |
| private | β
| β | β | β |
| default | β
| β
| β | β |
| protected | β
| β
| β
| β* |
| public | β
| β
| β
| β
|
(*Protected works in another package only through inheritance)
π» Example Used in My Code
I created a class called Bala with variables using different access modifiers.
package developer;
public class Bala {
private int salary = 10000;
String work = "Development";
protected String headphone = "sony";
public String Designation = "Developer";
public static void main(String[] args) {
Bala bala1 = new Bala();
System.out.println(bala1.salary);
}
}
| Variable | Access Modifier | Access Level |
| ----------- | --------------- | -------------------------- |
| salary | private | Only inside the same class |
| work | default | Same package only |
| headphone | protected | Same package + subclass |
| Designation | public | Anywhere |
Example:
package developer;
public class Nantha extends Bala {
public static void main(String[] args) {
Nantha NanthaKumar = new Nantha();
System.out.println(NanthaKumar.work);
System.out.println(NanthaKumar.headphone);
System.out.println(NanthaKumar.Designation);
}
}
Accessible
β default variable (work)
β protected variable (headphone)
β public variable (Designation)
Not Accessible
β private variable (salary)
π Access From Different Package
Example:
package tester;
import developer.Bala;
public class Deepika extends Bala {
public static void main(String[] args) {
Deepika deepika = new Deepika();
System.out.println(deepika.headphone);
System.out.println(deepika.Designation);
}
}
Accessible
β protected (through inheritance)
β public
Not Accessible
β private
β default
π§βπ» Access Using Object in Another Package
package tester;
import developer.Bala;
public class Kumar extends Bala {
public static void main(String[] args) {
Kumar kumar = new Kumar();
System.out.println(kumar.Designation);
Bala bala = new Bala();
System.out.println(bala.Designation);
}
}
Here:
β public variable is accessible everywhere.
β οΈ Exceptional Scenarios in Encapsulation
These are important situations where access modifiers behave differently.
1οΈβ£ Private Variable Access
Private variables cannot be accessed outside the class.
Example (Error):
NanthaKumar.salary
β Solution: Use Getter and Setter methods.
2οΈβ£ Default Modifier Limitation
Default variables cannot be accessed outside the package.
Example:
System.out.println(deepika.work);
β Compilation error.
3οΈβ£ Protected Access in Different Package
Protected variables are accessible only through inheritance.
Example:
Deepika extends Bala
β Accessible.
But:
Bala b = new Bala();
b.headphone
β Not accessible.
4οΈβ£ Public Access
Public members can be accessed from anywhere in the program.
Example:
System.out.println(bala.Designation);
β Works everywhere.
5οΈβ£ Private Access Through Getter & Setter
In real-world projects, private variables are accessed through getter and setter methods.
Example:
private int salary;
public int getSalary(){
return salary;
}
This is the real implementation of encapsulation.
π§ Key Takeaways
β Encapsulation protects data
β Access modifiers control visibility
β Private variables improve security
β Getter & Setter methods provide controlled access
β Encapsulation is widely used in automation frameworks
π¨βπ« Trainer: Nantha from Payilagam
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)