Inheriting Methods
Override method:
Supply a different implementation of a method that exists in the superclass
Must have same signature (same name and same parameter types)
If method is applied to an object of the subclass type, the overriding method is executed
Inherit method:
Don't supply a new implementation of a method that exists in superclass
Superclass method can be applied to the subclass objects
Add method:
Supply a new method that doesn't exist in the superclass
New method can be applied only to subclass objects
Inheriting Instance Fields
Can't override fields
Inherit field: All fields from the superclass are automatically inherited
Add field: Supply a new field that doesn't exist in the superclass
What if you define a new field with the same name as a superclass field?
Each object would have two instance fields of the same name
Fields can hold different values
Legal but extremely undesirable
Implementing the CheckingAccount Class
Overrides deposit and withdraw to increment the transaction count:
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
}
Each CheckingAccount object has two instance fields:
balance (inherited from BankAccount)
transactionCount (new to CheckingAccount)
You can apply four methods to CheckingAccount objects:
getBalance() (inherited from BankAccount)
deposit(double amount) (overrides BankAccount method)
withdraw(double amount) (overrides BankAccount method)
deductFees() (new to CheckingAccount)
Inherited Fields are Private
Consider deposit method of CheckingAccount
public void deposit(double amount)
{
transactionCount++;
// now add amount to balance
. . .
}
Can't just add amount to balance
balance is a private field of the superclass
A subclass has no access to private fields of its superclass
Subclass must use public interface
Invoking a Superclass Method
Can't just call
deposit(amount)
in deposit method of CheckingAccount
That is the same as
this.deposit(amount)
Calls the same method (infinite recursion)
Instead, invoke superclass method
super.deposit(amount)
Now calls deposit method of BankAccount class
Complete method:
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.