Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP), alongside inheritance, polymorphism, and abstraction. Encapsulation is the mechanism of restricting direct access to some of an object’s components and ensuring the internal representation of an object is hidden from the outside. It is achieved by bundling the data (variables) and the methods (functions) that manipulate the data into a single unit called a class.
Encapsulation in Java is implemented using:
private
: The member is accessible only within the same class.default
(no modifier): The member is accessible within the same package.protected
: The member is accessible within the same package and subclasses.public
: The member is accessible from any other class.public class Person {
// Private fields
private String name;
private int age;
// Public getter for name
public String getName() {
return name;
}
// Public setter for name
public void setName(String name) {
this.name = name;
}
// Public getter for age
public int getAge() {
return age;
}
// Public setter for age
public void setAge(int age) {
if (age > 0) { // Simple validation
this.age = age;
}
}
}
public class Person {
// Private fields
private String name;
private int age;
// Public getter for name
public String getName() {
return name;
}
// Public setter for name
public void setName(String name) {
this.name = name;
}
// Public getter for age
public int getAge() {
return age;
}
// Public setter for age
public void setAge(int age) {
if (age > 0) { // Simple validation
this.age = age;
}
}
}
In the example above, the fields name
and age
are private, which means they cannot be accessed directly from outside the Person
class. Instead, the getName
, setName
, getAge
, and setAge
methods (public methods) are used to access and update these fields.
Consider a BankAccount
class that encapsulates the details of a bank account.
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}
In this example:
accountNumber
and balance
are private, ensuring that they cannot be modified directly.deposit
and withdraw
methods provide controlled access to modify the balance, including basic validation to prevent invalid operations.Encapsulation is a powerful feature in Java that enhances data security, modularity, and maintainability. By using access modifiers and providing public methods to access and modify private fields, encapsulation ensures that the internal state of an object is protected from unauthorized access and modification.