Java Lesson 24 – Encapsulation | Dataplexa

Encapsulation

Encapsulation is the process of wrapping data and methods together and controlling how they are accessed from outside a class. It is one of the most important principles of object-oriented programming in Java.

Encapsulation helps protect data from accidental misuse and makes programs easier to understand, maintain, and extend.


Real-World Understanding of Encapsulation

Think about a bank account. You cannot directly change your account balance. Instead, you deposit or withdraw money using proper methods.

The balance is hidden, and access is controlled. This is exactly how encapsulation works in Java.


Why Encapsulation Is Important

Encapsulation allows Java developers to:

  • Protect sensitive data
  • Prevent invalid values
  • Improve code readability
  • Reduce dependency between classes

Almost every professional Java application uses encapsulation to maintain data integrity.


How Encapsulation Is Implemented in Java

Encapsulation is implemented using:

  • Private variables to hide data
  • Public methods to access or modify data

These public methods are commonly called getters and setters.


Example Without Encapsulation (Problem)

Here is an example where data is directly accessible. This approach is unsafe.


class BankAccount {
    double balance;
}

Anyone can change the balance directly, which can cause serious issues.


Applying Encapsulation (Solution)

Now let’s protect the data using encapsulation.


class BankAccount {

    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}

Now the balance can only be modified using controlled methods.


Using the Encapsulated Class

Let’s see how this works in practice.


public class Main {
    public static void main(String[] args) {

        BankAccount account = new BankAccount();

        account.deposit(5000);
        account.withdraw(1500);

        System.out.println("Current Balance: " + account.getBalance());
    }
}

This approach ensures that the account balance always stays valid.


Benefits of Encapsulation in Real Projects

Encapsulation is heavily used in:

  • Banking and financial systems
  • Employee management applications
  • Enterprise software
  • Framework and library design

It allows developers to change internal logic without affecting external code.


Encapsulation vs Data Hiding

Data hiding is part of encapsulation. Encapsulation goes one step further by combining data and behavior together.

Java access modifiers like private, protected, and public help achieve this control.


Key Takeaways

  • Encapsulation protects internal data
  • It uses private variables and public methods
  • It improves code safety and maintainability
  • It is essential for real-world Java applications

In the next lesson, we will explore packages, which help organize large Java projects effectively.