Subclass Construction
super followed by a parenthesis indicates a call to the superclass constructor
public class CheckingAccount extends BankAccount
{
public CheckingAccount(double initialBalance)
{
// Construct superclass
super(initialBalance);
// Initialize transaction count
transactionCount = 0;
}
. . .
}
Must be the first statement in subclass constructor
If subclass constructor doesn't call superclass constructor, default superclass constructor is used
Default constructor: constructor with no parameters
If all constructors of the superclass require parameters, then the compiler reports an error
Why didn't the SavingsAccount constructor in Section 10.1 call its superclass constructor?
Answer: It was content to use the default constructor of the superclass, which sets the balance to zero.
When you invoke a superclass method with the super keyword, does the call have to be the first statement of the subclass method?
Answer: No – this is a requirement only for constructors. For example, the SavingsAccount.deposit method first increments the transaction count, then calls the superclass method.