Encapsulation Example - Bank Account Class

3 minute read

In this tutorial, we’ll explore the concept of encapsulation through a practical example of designing a Bank Account class in Java. Encapsulation is a fundamental principle of object-oriented programming that involves bundling data and methods within a class and controlling access to that data to ensure data security and integrity.

Designing the Bank Account Class

To begin, let’s design the structure of our Bank Account class. It will include attributes such as account number, account holder name, account balance, and methods to perform transactions like deposit and withdrawal.

Encapsulating Account Data

We’ll encapsulate the account data by declaring the attributes of the Bank Account class as private. This ensures that the data can only be accessed and modified through controlled methods, preventing unauthorized access and manipulation.

Implementing Accessor and Mutator Methods

Accessor methods, also known as getters, will be implemented to retrieve the values of private attributes, while mutator methods, or setters, will be used to modify those values. This encapsulates the data and provides controlled access to it.

Performing Transactions

We’ll define methods to perform transactions such as deposit and withdrawal. These methods will include validation checks to ensure that transactions are performed accurately and securely, maintaining data integrity.

Ensuring Data Integrity and Security

By encapsulating the account data and providing controlled access through methods, we ensure that the data remains consistent and secure. Encapsulation prevents direct manipulation of account attributes, reducing the risk of errors and unauthorized access.

Example Code Implementation

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

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

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

    public String getAccountHolderName() {
        return accountHolderName;
    }

    public double getBalance() {
        return balance;
    }

    // Method to deposit funds
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Deposit of $" + amount + " successful. New balance: $" + balance);
        } else {
            System.out.println("Invalid amount for deposit.");
        }
    }

    // Method to withdraw funds
    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            System.out.println("Withdrawal of $" + amount + " successful. New balance: $" + balance);
        } else {
            System.out.println("Insufficient funds or invalid amount for withdrawal.");
        }
    }
}

BankAccount Class Attributes:

  • private int accountNumber: This attribute stores the unique account number of the bank account. It’s private to ensure that it can only be accessed and modified within the BankAccount class itself.
  • private String accountHolderName: This attribute stores the name of the account holder. Like accountNumber, it’s also private for data security.
  • private double balance: This attribute represents the current balance of the bank account. It’s also private to enforce encapsulation.

Constructor:

  • public BankAccount(int accountNumber, String accountHolderName, double balance): This is the constructor of the BankAccount class. It initializes the account with the provided account number, account holder name, and initial balance.

Getter Methods:

  • public int getAccountNumber(): This method returns the account number of the bank account.
  • public String getAccountHolderName(): This method returns the account holder’s name.
  • public double getBalance(): This method returns the current balance of the bank account.

These getter methods provide controlled access to the private attributes of the BankAccount class.

Transaction Methods:

  • public void deposit(double amount): This method allows the account holder to deposit funds into the bank account. It takes the amount to deposit as a parameter and updates the balance accordingly. If the amount is valid (greater than zero), it adds the amount to the balance and prints a success message along with the new balance. Otherwise, it prints an error message for an invalid amount.
  • public void withdraw(double amount): This method allows the account holder to withdraw funds from the bank account. It takes the amount to withdraw as a parameter and updates the balance accordingly. If the amount is valid (greater than zero and less than or equal to the current balance), it deducts the amount from the balance and prints a success message along with the new balance. Otherwise, it prints an error message for insufficient funds or an invalid amount.

Conclusion:

Overall, this BankAccount class demonstrates encapsulation by keeping the account data private and providing controlled access to it through getter methods. It also ensures data integrity and security by validating transactions and preventing unauthorized access to sensitive information.

Updated: