1. What is Encapsulation?
Encapsulation is one of the four core OOP concepts (along with Inheritance, Polymorphism, and Abstraction).
It is the mechanism of restricting direct access to some of an object’s components and providing controlled access through methods.
Key points:
- Data (fields) of a class are made private.
- Access to these fields is provided via public getter and setter methods.
- This helps control how the data is accessed or modified, improving security and maintainability.
2. Why use Encapsulation?
Encapsulation provides several benefits:
- Data Hiding – Prevents external classes from directly modifying sensitive data.
- Control Access – You can validate inputs before modifying a variable.
- Flexibility – Internal implementation can change without affecting external code.
- Improved Maintainability – Changes in one class do not affect others if encapsulation is properly used.
3. How Encapsulation Works
Encapsulation works by:
- Declaring class variables as
private
. - Providing
public
getter and setter methods to access or modify these variables. - Optionally, applying logic in setters to validate or restrict data.
4. Example: Encapsulation in a POJO
Here’s a simple example of a POJO with encapsulation:
public class Student {
// Step 1: Make fields private
private String name;
private int age;
// Step 2: Provide public getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
// Step 3: Provide public setters with validation
public void setName(String name) {
if (name != null && name.length() > 0) {
this.name = name;
} else {
System.out.println("Invalid name.");
}
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
} else {
System.out.println("Age must be positive.");
}
}
}
5. Using the Encapsulated Class
public class Main {
public static void main(String[] args) {
Student student = new Student();
// Trying to directly access the fields (won't work)
// student.name = "John"; // ERROR: name has private access
// Using setter methods
student.setName("John"); // Allowed
student.setAge(25); // Allowed
student.setAge(-5); // Rejected due to validation
// Using getter methods
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
Output:
Age must be positive.
Student Name: John
Student Age: 25
6. How it Restricts Data
- Direct modification is blocked:
private
keyword prevents other classes from accessing the variables. - Controlled modification: The setter validates the data before setting it.
- Read-only or write-only access: You can provide only getter (read-only) or setter (write-only) if needed.
Example: Read-only field
private final String studentId; // Cannot be changed once assigned
public String getStudentId() {
return studentId;
}
// No setter method, so it’s read-only
7. Real-world analogy
Think of encapsulation like a bank account:
- Your balance is private.
- You can’t just change it directly.
- You deposit or withdraw through controlled methods.
- The bank validates your transactions before updating your balance.