Java Programming Handbook

    Encapsulation in Java

    Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP). It is the concept of wrapping data (variables) and methods that operate on the data into a single unit called a class. It helps in restricting direct access to certain details of an object and only exposing necessary information through methods.

    Why Encapsulation?#

    • Data Hiding: It prevents direct access to class fields, ensuring better control over data.
    • Modularity: A class with encapsulated fields and methods makes it easier to manage code.
    • Maintainability: Changing the internal implementation does not affect external code.
    • Security: Restricts unauthorized access to sensitive data.

    How to Implement Encapsulation?#

    Encapsulation in Java is implemented by:

    • Declaring class variables as private.
    • Providing public getter and setter methods to access and update the private fields.

    Example: Bank Account#

    Let's consider a bank account where we encapsulate the balance to ensure controlled access.

    // BankAccount.java class BankAccount { private double balance; // private variable // Constructor public BankAccount(double initialBalance) { if (initialBalance > 0) { this.balance = initialBalance; } } // Getter method to access balance public double getBalance() { return balance; } // Method to deposit money public void deposit(double amount) { if (amount > 0) { balance += amount; System.out.println("Deposited: " + amount); } else { System.out.println("Invalid deposit amount."); } } // Method to withdraw money public void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; System.out.println("Withdrawn: " + amount); } else { System.out.println("Insufficient funds or invalid amount."); } } } // Main class to test Encapsulation public class Main { public static void main(String[] args) { // Create a bank account object BankAccount account = new BankAccount(5000); // Display initial balance System.out.println("Initial Balance: " + account.getBalance()); // Deposit money account.deposit(2000); System.out.println("Updated Balance: " + account.getBalance()); // Withdraw money account.withdraw(1000); System.out.println("Final Balance: " + account.getBalance()); } }

    Expected Output#

    Initial Balance: 5000.0 Deposited: 2000.0 Updated Balance: 7000.0 Withdrawn: 1000.0 Final Balance: 6000.0

    How Encapsulation Is Achieved in This Example#

    1. The balance field is declared private, so it can't be accessed directly from outside the class.
    2. The getBalance(), deposit(), and withdraw() methods provide controlled access to the field.
    3. Both deposit() and withdraw() include validation logic, ensuring that incorrect values cannot affect the internal state.

    Conclusion#

    Encapsulation is a powerful mechanism in Java that ensures data security and better maintainability of code. By restricting direct access to fields and using methods to modify them, we can achieve controlled access and avoid unintended modifications. This concept is widely used in real-world applications to protect sensitive data and enhance modularity.

    Last updated on Apr 09, 2025