Java Encapsulation Explained — Quick Guide

Alexander Obregon
2 min readApr 24, 2023

--

Image Source

Introduction

Encapsulation is one of the four fundamental principles of object-oriented programming (OOP). It refers to the process of bundling data and methods that operate on that data within a single unit, such as a class. In Java, encapsulation is achieved by using access modifiers (private, protected, and public) and using getter and setter methods. In this article, we will explore the concept of encapsulation in Java, its benefits, and how to implement it using code examples.

Benefits of Encapsulation

  1. Data Hiding: By encapsulating data, we can hide the internal state of an object from other objects, which prevents unauthorized access or modification of the object’s state.
  2. Modularity: Encapsulation allows breaking down a large application into smaller, manageable modules, making it easier to maintain and update.
  3. Flexibility and Maintainability: Encapsulation enables developers to modify the implementation details of a class without affecting the code that depends on it.

Implementing Encapsulation in Java

Let’s consider a simple example to demonstrate encapsulation in Java. We will create a class called ‘BankAccount’ that will store account-related information and provide methods to perform operations on the account.

public class BankAccount {
private String accountNumber;
private double balance;

// Constructor
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}

// Getter methods
public String getAccountNumber() {
return accountNumber;
}

public double getBalance() {
return balance;
}

// Setter method
public void setBalance(double balance) {
if (balance >= 0) {
this.balance = balance;
} else {
System.out.println("Invalid balance.");
}
}

// Methods for account operations
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
} else {
System.out.println("Invalid deposit amount.");
}
}

public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Invalid withdrawal amount.");
}
}
}

In this example, we have marked the ‘accountNumber’ and ‘balance’ fields as ‘private’, ensuring that they can only be accessed within the ‘BankAccount’ class. We provide getter methods for these fields to allow read access and a setter method for the ‘balance’ field to allow controlled write access.

Here’s how to use the ‘BankAccount’ class:

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount("1234567890", 1000);

System.out.println("Account number: " + account.getAccountNumber());
System.out.println("Current balance: " + account.getBalance());

account.deposit(500);
System.out.println("Balance after deposit: " + account.getBalance());

account.withdraw(300);
System.out.println("Balance after withdrawal: " + account.getBalance());
}
}

Conclusion

Encapsulation is a powerful concept in Java that promotes data hiding, modularity, and maintainability. By using access modifiers and getter and setter methods, we can control access to an object’s state and ensure that only valid modifications are made. As a result, encapsulation plays a crucial role in building strong and modular Java applications.

--

--

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn: https://www.linkedin.com/in/alexander-obregon-97849b229/