Java Encapsulation
What is Encapsulation in Java?
Encapsulation is one of the core principles of Object-Oriented
Programming (OOP). It refers to wrapping data (variables) and code
(methods) into a single unit (class) and restricting direct access to that
data from outside the class.
In simple terms, encapsulation ensures that sensitive data is hidden and
controlled through methods rather than being accessed directly.
How Encapsulation is Achieved in Java
To implement encapsulation in Java:
- Declare class variables as private
- Provide public getter and setter methods
- Access data only through these methods
Getter and Setter Methods
- Getter → Returns the value of a private variable
- Setter → Updates the value of a private variable
Naming convention:
getVariableName()
setVariableName()
Example:
Variable → salary
Methods → getSalary() and setSalary()
Encapsulation Example in Java
Step 1: Create an Encapsulated Class
class Employee {
private String name;
private double salary;
// Getter for name
public String getName() {
return name;
}
// Setter for name
public void setName(String empName) {
name = empName;
}
// Getter for salary
public double getSalary() {
return salary;
}
// Setter for salary
public void setSalary(double empSalary) {
if (empSalary > 0) {
salary = empSalary;
}
}
}
Step 2: Access Data via Methods
public class EncapsulationDemo {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setName("Arun Kumar");
emp.setSalary(45000);
System.out.println("Employee: " +
emp.getName());
System.out.println("Salary: " +
emp.getSalary());
}
}
Output
Employee: Arun Kumar
Salary: 45000.0
Why Private Variables Cannot Be Accessed Directly
If we try to access a private variable outside its class:
Employee emp = new Employee();
emp.name = "Arun"; // ❌ Compilation error
Java shows an error because private members are accessible only within
the same class.
This restriction is the foundation of encapsulation.
Real-World Encapsulation Example
Encapsulation is widely used in banking systems where account data must
be protected.
class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount <= balance && amount > 0) {
balance -= amount;
}
}
}
Usage:
public class BankApp {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
account.withdraw(300);
System.out.println("Remaining Balance: " +
account.getBalance());
}
}
Advantages of Encapsulation in Java
1. Data Security
Sensitive data cannot be accessed directly from outside the class.
2. Controlled Access
Validation logic can be applied in setters before updating values.
Example:
if (age >= 18) this.age = age;
3. Flexibility and Maintainability
Internal implementation can change without affecting external code.
4. Read-Only or Write-Only Fields
- Only getter → read-only
- Only setter → write-only
Read-Only Encapsulation Example
class Product {
private final int id = 101;
public int getId() {
return id;
}
}
No setter → value cannot be changed.
Write-Only Encapsulation Example
class SecretCode {
private String code;
public void setCode(String c) {
code = c;
}
}
No getter → value cannot be viewed.