Java Reference
In-Depth Information
transactionCount++;
super.deposit(amount);
}
Purpose:
To call a method of the superclass instead of the method of the current class
S
ELF
C
HECK
6.
Why does the
withdraw
method of the
CheckingAccount
class
call
super.withdraw?
7.
Why does the
deductFees
method set the transaction count to zero?
C
OMMON
E
RROR
10.2: Shadowing Instance Fields
A subclass has no access to the private instance fields of the superclass. For
example, the methods of the
CheckingAccount
class cannot access the
balance
field:
public class CheckingAccount extends BankAccount
{
public void deposit(double amount)
{
transactionCount++;
balance = balance + amount; // Error
}
. . .
}
It is a common beginner's error to Ȓsolveȓ this problem by adding another instance
field with the same name.
public class CheckingAccount extends BankAccount
{
public void deposit(double amount)
{
transactionCount++;
balance = balance + amount;
}
. . .
449
450