Java: Encapsulation Explained

Dhesinghu Alagarsamy
2 min readNov 9, 2023

--

“The Locked Treasure: Unveiling Java Encapsulation”

In a small medieval village, there was a treasure chest with valuable coins, gems, and other precious items. The villagers were cautious about protecting this treasure from thieves. To safeguard it, they placed the chest inside a sturdy wooden box and locked it with a magical key.

The key was the only way to access the treasure, and it could only be used by the trusted village treasurer. Whenever someone wanted to add or take something from the chest, they had to request the treasurer to use the key for them.

This arrangement ensured that the treasure remained secure. No one from outside the village could access it directly, and only the treasurer had control over it. The contents of the treasure chest were hidden and protected from unauthorized access.

Explanation:

In Java, encapsulation is a fundamental concept that involves bundling data (attributes) and the methods (functions) that operate on that data into a single unit, known as a class. It also involves controlling access to that data and methods to ensure they are used correctly. Encapsulation helps protect data from unauthorized access and modification.

Sample Code:

Here’s a Java code example to illustrate encapsulation:

public class TreasureChest {
private int coins;
private String gems;

public void addCoins(int amount) {
if (amount > 0) {
coins += amount;
}
}

public int getCoins() {
return coins;
}

public void addGems(String gem) {
if (gem != null && !gem.isEmpty()) {
gems = gem;
}
}

public String getGems() {
return gems;
}
}

public class Village {
public static void main(String[] args) {
TreasureChest chest = new TreasureChest();
chest.addCoins(1000);
chest.addGems("Diamonds");

// Accessing data through methods
int coins = chest.getCoins();
String gems = chest.getGems();

System.out.println("Coins in the treasure: " + coins);
System.out.println("Gems in the treasure: " + gems);
}
}

In this example, the TreasureChest class encapsulates the data (coins and gems) and methods (addCoins, getCoins, addGems, getGems). The attributes are marked as private, making them inaccessible directly from outside the class. Access is controlled through public methods, ensuring that data can only be modified and accessed in specific ways.

Encapsulation in programming is the practice of bundling the data (attributes) and the methods (functions) that operate on that data into a single unit called a class. Access to the data is controlled by defining the attributes as private and providing public methods (getters and setters) for interacting with the data. This safeguards the data from unauthorized access and modification while allowing controlled and consistent interaction with it. Encapsulation enhances data security, maintainability, and code organization.

--

--

Dhesinghu Alagarsamy

Blockchain architect with extensive experience in digital assets, Ethereum, Hyperledger, and enterprise app development leadership.