Encapsulation is the process of **hiding the internal details** of an object and only exposing necessary parts. It is achieved using **access modifiers**.
Encapsulation in Java
What is Encapsulation?
Encapsulation is the process of **hiding the internal details** of an object and only exposing necessary parts. It is achieved using **access modifiers**.
Why Use Encapsulation?
- Protects data from direct access.
- Improves code maintainability.
- Provides better control over data.
- Enhances flexibility and reusability.
How to Achieve Encapsulation in Java?
- Declare class variables as **private**.
- Provide **public getters and setters** to access or update values.
Encapsulation Example
In this example, the Person
class has **private variables** and provides **public getter and setter methods**.
class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter for name
public String getName() {
return name;
}
// Setter for name
public void setName(String name) {
this.name = name;
}
// Getter for age
public int getAge() {
return age;
}
// Setter for age
public void setAge(int age) {
if (age > 0) {
this.age = age;
} else {
System.out.println("Age must be positive!");
}
}
}
public class EncapsulationExample {
public static void main(String[] args) {
Person person = new Person("Alice", 25);
// Access data through getters
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
// Modify data using setters
person.setAge(30);
System.out.println("Updated Age: " + person.getAge());
}
}
Access Modifiers in Java
Encapsulation uses **access modifiers** to control visibility:
Modifier | Within Class | Within Package | Outside Package (Subclass) | Outside Package |
---|---|---|---|---|
private |
✔️ | ❌ | ❌ | ❌ |
default |
✔️ | ✔️ | ❌ | ❌ |
protected |
✔️ | ✔️ | ✔️ | ❌ |
public |
✔️ | ✔️ | ✔️ | ✔️ |
Real-World Example: Banking System
Encapsulation ensures secure account data handling in a **Bank Account** system.
class BankAccount {
private double balance;
// Constructor
public BankAccount(double balance) {
this.balance = balance;
}
// Getter for balance
public double getBalance() {
return balance;
}
// Deposit method
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
} else {
System.out.println("Deposit amount must be positive!");
}
}
// Withdraw method
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Insufficient balance or invalid amount!");
}
}
}
public class BankExample {
public static void main(String[] args) {
BankAccount account = new BankAccount(500.00);
// Accessing balance safely
System.out.println("Current Balance: $" + account.getBalance());
// Performing transactions
account.deposit(200.00);
account.withdraw(100.00);
System.out.println("Updated Balance: $" + account.getBalance());
}
}
Key Takeaways
- Encapsulation **hides data** and ensures controlled access.
- Use **private fields** and **public getters & setters**.
- Enhances **security, maintainability, and flexibility**.
📌 Next Topic: Exception Handling in Java