Encapsulation is the process of wrapping data (variables) and methods (functions) into a single unit — a class. It is one of the fundamental pillars of OOP.
It also ensures data hiding, meaning internal details of an object are hidden from the outside world.
In simple terms:
- Keep data private
- Provide public getters and setters to access/modify it safely
-
Data Protection Prevents direct access to sensitive fields.
-
Controlled Access You can validate values before setting them.
-
Loose Coupling Internal implementation can change without affecting other code.
-
Better Maintainability Focus on clean, modular design.
Encapsulation is implemented using:
- Private variables
- Public getters and setters
class BankAccount {
private double balance; // hidden data
private String accountHolder;
public double getBalance() { // getter
return balance;
}
public void setBalance(double balance) { // setter with validation
if (balance >= 0) {
this.balance = balance;
}
}
public String getAccountHolder() {
return accountHolder;
}
public void setAccountHolder(String name) {
if (name != null && !name.isEmpty()) {
this.accountHolder = name;
}
}
}Usage:
BankAccount acc = new BankAccount();
acc.setBalance(5000);
System.out.println(acc.getBalance());These concepts are related but not identical.
| Concept | Meaning |
|---|---|
| Data Hiding | Restricting access to internal data using private modifiers |
| Encapsulation | Grouping data + operations inside a class |
Encapsulation achieves data hiding.
+----------------------------+
| BankAccount |
+----------------------------+
| private double balance | ← Hidden from outside
| private String holderName |
+----------------------------+
| getBalance() |
| setBalance() | ← Controlled access
+----------------------------+
External classes cannot directly modify variables:
acc.balance = -500; // ❌ Not allowedThey must use setters:
acc.setBalance(1000); // ✔ Controlled accessclass Student {
private String name;
private int age;
public void setAge(int age) {
if (age > 0 && age < 100) { // validation
this.age = age;
}
}
public int getAge() {
return age;
}
public void setName(String name) {
if (name != null && !name.isBlank()) {
this.name = name;
}
}
public String getName() {
return name;
}
}By using getters and setters, the class maintains internal consistency.
| Advantage | Description |
|---|---|
| Security | Sensitive data cannot be accessed directly |
| Flexibility | Change implementation without affecting users |
| Validation | Enforce restrictions on data |
| Maintainability | Clean and organized code |
| Reusability | Encapsulated components can be reused easily |
Java uses encapsulation heavily:
-
Stringclass → privatevalue[] -
ArrayList→ privateelementData[] -
HashMap→ private internal table
Example (from JDK source):
public class String {
private final byte[] value;
}Operations are performed through public methods — not by modifying internal fields.
Encapsulation is the wrapping of data and methods into a single unit, and restricting direct access to data using access modifiers.
By keeping fields private and exposing only safe operations.
Yes, technically — any class with private fields is encapsulated. Getters/setters simply provide controlled access.
- Encapsulation → How data is protected
- Abstraction → What details are shown/hidden from user
To prevent unauthorized or invalid modifications.
-
Encapsulation hides internal data and exposes only needed details.
-
It is achieved using private fields + public getters & setters.
-
It ensures security, validation, and cleaner code.
-
Major Java classes internally rely on encapsulation.
-
Encapsulation does not mean only data hiding — it includes bundling data + behavior.