previous | start | next

Creating a subclass: SavingsAccount

public class SavingsAccount extends BankAccount
{
  private double interestRate;
  
  public SavingsAccount(double amount, double rate)
  {
    super(amount);
    interestRate = rate;
  }

  public void postInterest()
  {
    double balance = getBalance();
    double interest = balance * interestRate/100;
    deposit(interest);
  }
  public double getRate()
  {
    return interestRate;
  }
  void setRate(double newRate)
  {
    interestRate = newRate;
  }

  public String toString()
  {
    return String.format("Savings Account(balance = %.2f, rate = %.3f)",
                               getBalance(), interestRate);
  }
}

   


previous | start | next