Java Reference
In-Depth Information
S YNTAX 10.3 Calling a Superclass Constructor
accessSpecifier ClassName(parameterType parameterName, . . .)
{
super (parameters);
. . .
}
Example:
public CheckingAccount(double initialBalance)
{
super(initialBalance);
transactionCount = 0;
}
Purpose:
To invoke the constructor of the superclass. Note that this statement must be the
first statement of the subclass constructor.
451
452
The dual use of the super keyword is analogous to the dual use of the this
keyword (see Advanced Topic 3.1 ).
If a subclass constructor does not call the superclass constructor, the superclass is
constructed with its default constructor (that is, the constructor that has no
parameters). However, if all constructors of the superclass require parameters, then
the compiler reports an error.
For example, you can implement the CheckingAccount constructor without
calling the superclass constructor. Then the BankAccount class is constructed with
its default constructor, which sets the balance to zero. Of course, then the
CheckingAccount constructor must explicitly deposit the initial balance.
Most commonly, however, subclass constructors have some parameters that they pass
on to the superclass and others that they use to initialize subclass fields.
Search WWH ::




Custom Search