Inheriting Methods

Inheriting Instance Fields

Implementing the CheckingAccount Class

   public class CheckingAccount extends BankAccount
{
public void deposit(double amount) { . . . }
public void withdraw(double amount) { . . . }
public void deductFees() { . . . }
// new method private int transactionCount; // new instance field
}

Inherited Fields are Private

public void deposit(double amount)
 {
    transactionCount++;
    // now add amount to balance
    . . .
}

Invoking a Superclass Method

 deposit(amount)

 in deposit method of CheckingAccount  

this.deposit(amount)

super.deposit(amount)

public void deposit(double amount)
{
   transactionCount++;
   // Now add amount to balance
   super.deposit(amount);
}

Implementing Remaining Methods

public class CheckingAccount extends BankAccount
{
   . . .
   public void withdraw(double amount)
   {
      transactionCount++;
      // Now subtract amount from balance  
     
super.withdraw(amount);
   }

   public void deductFees()
   {
      if (transactionCount > FREE_TRANSACTIONS)
      {
         double fees = TRANSACTION_FEE
            * (transactionCount - FREE_TRANSACTIONS);
        
super.withdraw(fees);
      }

      transactionCount = 0;
   }
   . . .
   private static final
   int FREE_TRANSACTIONS = 3;
   private static final double TRANSACTION_FEE = 2.0;
}


Why does the withdraw method of the CheckingAccount class call super.withdraw?

   Answer: It needs to reduce the balance, and it cannot access the balance field directly.


Why does the deductFees method set the transaction count to zero?

   Answer: So that the count can reflect the number of transactions for the following month.