w3resource

Java Polymorphism: BankAccount base class with Savings, Checking Account subclasses

Java Polymorphism: Exercise-9 with Solution

Write a Java program to create a base class BankAccount with methods deposit() and withdraw(). Create two subclasses SavingsAccount and CheckingAccount. Override the withdraw() method in each subclass to impose different withdrawal limits and fees.

Sample Solution:

Java Code:

//BankAccount.java
class BankAccount {
  private double balance;

  public BankAccount(double initialBalance) {
    this.balance = initialBalance;
  }

  public double getBalance() {
    return balance;
  }

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

  public void withdraw(double amount) {
    if (amount <= balance) {
      balance -= amount;
    } else {
      System.out.println("Insufficient funds.");
    }
  }
}

//SavingsAccount.java

class SavingsAccount extends BankAccount {
  private double withdrawalLimit;

  public SavingsAccount(double initialBalance, double withdrawalLimit) {
    super(initialBalance);
    this.withdrawalLimit = withdrawalLimit;
  }

  @Override
  public void withdraw(double amount) {
    if (amount <= withdrawalLimit) {
      super.withdraw(amount);
    } else {
      System.out.println("Withdrawal limit exceeded.");
    }
  }
}
//CheckingAccount.java
class CheckingAccount extends BankAccount {
  private double overdraftFee;

  public CheckingAccount(double initialBalance, double overdraftFee) {
    super(initialBalance);
    this.overdraftFee = overdraftFee;
  }

  @Override
  public void withdraw(double amount) {
    if (amount <= getBalance()) {
      super.withdraw(amount);
    } else {
      System.out.println("Overdraft fee applied.");
      super.withdraw(amount + overdraftFee);
    }
  }
}
//Main.java
public class Main {
  public static void main(String[] args) {
    BankAccount savingsAccount = new SavingsAccount(2000, 650);
    BankAccount checkingAccount = new CheckingAccount(1000, 100);

    withdrawFromAccount(savingsAccount, 300);
    withdrawFromAccount(checkingAccount, 250);

    System.out.println("Savings Account Balance: " + savingsAccount.getBalance());
    System.out.println("Checking Account Balance: " + checkingAccount.getBalance());
  }

  public static void withdrawFromAccount(BankAccount account, double amount) {
    account.withdraw(amount);
  }
}

Sample Output:

Savings Account Balance: 1700.0
Checking Account Balance: 750.0

Explanation:

In the above exercise -

  • The "BankAccount" class is the base class, and SavingsAccount and CheckingAccount are its subclasses. Each subclass overrides the withdraw() method to impose different withdrawal limits and fees based on their specific rules.
  • In the "Main" class, we have a static method withdrawFromAccount(BankAccount account, double amount) that takes an object of the base class BankAccount as a parameter. Inside this method, we call the withdraw() method on the account object. Since the withdrawFromAccount method takes a BankAccount type parameter, it can accept SavingsAccount and CheckingAccount objects, thanks to polymorphism.

Flowchart:

Flowchart: BankAccount Java
Flowchart: SavingsAccount Java
Flowchart: CheckingAccount Java
Flowchart: Main Java

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Shape base class with Circle, Square, and Triangle subclasses.
Next: Animal base class with Lion, Tiger, and Panther subclasses.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.