Activity Title: Secure Bank Account – Implementing Encapsulation in Java
Objective:
Understand and apply the concept of encapsulation in Java.
Use private variables and getter/setter methods to control data access.
Learn the importance of data hiding and security in real-world applications.
Materials Needed:
Computer with Java Development Kit (JDK) installed
Java IDE (Eclipse, IntelliJ, or VS Code)
Activity Instructions:
Step 1: Create a New Java Project
1. Open your Java IDE and create a new Java project named EncapsulationDemo.
2. Inside the project, create a new class called BankAccount.
Step 2: Implement Encapsulation
In BankAccount.java, declare the account number, account holder's name, and
balance as private variables.
Create getter and setter methods for controlled access.
// BankAccount.java
class BankAccount {
private String accountNumber;
private String accountHolder;
private double balance;
// Constructor
public BankAccount(String accountNumber, String accountHolder, double
balance) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = balance;
}
// Getter for account number
public String getAccountNumber() {
return accountNumber;
}
// Getter for account holder
public String getAccountHolder() {
return accountHolder;
}
// Getter for balance (no setter to prevent direct modification)
public double getBalance() {
return balance;
}
// Method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit successful! New balance: " + balance);
} else {
System.out.println("Invalid deposit amount.");
}
}
// Method to withdraw money with security check
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawal successful! New balance: " +
balance);
} else {
System.out.println("Insufficient balance or invalid amount.");
}
}
}
Step 3: Test the Encapsulation in a Main Class
Create a BankApp.java file and write a main method to test the encapsulated
BankAccount class.
java
CopyEdit
// BankApp.java
public class BankApp {
public static void main(String[] args) {
// Creating a new bank account
BankAccount account = new BankAccount("123456789", "Alice Johnson",
5000.00);
// Display initial details
System.out.println("Account Holder: " + account.getAccountHolder());
System.out.println("Account Number: " + account.getAccountNumber());
System.out.println("Current Balance: " + account.getBalance());
// Attempting transactions
account.deposit(1500.00);
account.withdraw(2000.00);
account.withdraw(5000.00); // This should show an error
// Trying to modify balance directly (This should not be possible)
// account.balance = 10000; // Uncommenting this will cause an error
}
}
Discussion Questions:
1. Why are the accountNumber, accountHolder, and balance variables marked as
private?
2. What would happen if there were no getter and setter methods?
3. How does encapsulation improve security and data integrity?
4. Can we modify the balance directly? Why or why not?
5. How could we improve this class further (e.g., adding PIN-based access control)?