Encapsulation
Encapsulation is an object-oriented design principle that bundles data (variables) and behavior (methods) into a single unit (class) and restricts direct access to the data. It is achieved using access modifiers and getter/setter methods. This is a high-frequency interview topic and foundational for secure, maintainable Java design.
What Is Encapsulation?
- Wrapping data and methods into a single class
- Hiding internal state from external access
- Exposing controlled access via methods
In simple terms: Protect the data, expose behavior.
Why Encapsulation Is Important
- Improves data security
- Prevents invalid state changes
- Enhances maintainability
- Supports flexibility and refactoring
- Enables validation and business rules
How Encapsulation Is Achieved in Java
- Declare variables as private
- Provide public getter and setter methods
- Apply validation logic inside setters
Basic Encapsulation Example
class Student {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
}
Usage
Student s = new Student();
s.setAge(20);
System.out.println(s.getAge());
- Direct access is restricted
- Data is modified only through controlled methods
What Happens Without Encapsulation (Bad Design)
class Student {
public int age;
}
Student s = new Student();
s.age = -10; // invalid state
- ❌ No control
- ❌ No validation
- ❌ Poor design
Encapsulation vs Data Hiding (Clarification)
| Concept | Meaning |
|---|---|
| Encapsulation | Wrapping data + methods |
| Data Hiding | Restricting direct access using private |
- Encapsulation includes data hiding
- Data hiding alone is not full encapsulation
Access Modifiers and Encapsulation
| Modifier | Scope |
|---|---|
| private | Within class only |
| default | Same package |
| protected | Package + subclass |
| public | Everywhere |
Best Practice:
- Variables → private
- Methods → public (controlled access)
Encapsulation with Validation Logic
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
- Business rules enforced
- Invalid operations blocked
Read-Only and Write-Only Encapsulation
Read-Only Class
class Employee {
private int id = 101;
public int getId() {
return id;
}
}
Write-Only Class
class Password {
private String pwd;
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
Encapsulation and Immutability (Related Concept)
final class User {
private final String name;
User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
- Strong encapsulation
- Object state cannot change
Encapsulation Benefits in Real Projects
- Easier refactoring (internal change without breaking callers)
- Better debugging and testing
- Safer multi-team development
- Cleaner APIs and contracts
Common Beginner Mistakes
- Making variables public
- Writing getters/setters without validation
- Exposing internal objects directly
- Confusing encapsulation with abstraction
Interview-Ready Answers
Short Answer
Encapsulation is the process of wrapping data and methods into a single unit and restricting direct access to data.
Detailed Answer
In Java, encapsulation is achieved by declaring class variables as private and providing controlled access through public getter and setter methods. It improves security, prevents invalid data modification, and enhances maintainability.
Key Takeaway
Encapsulation protects object integrity. It enforces controlled access, valid states, and clean design, making Java applications robust and maintainable.