Java Reference
In-Depth Information
Triangle . The super( ) in Triangle calls the constructor in TwoDShape . In a class hier-
archy, if a superclass constructor requires parameters, then all subclasses must pass those
parameters “up the line.” This is true whether or not a subclass needs parameters of its own.
When Are Constructors Executed?
In the foregoing discussion of inheritance and class hierarchies, an important question may
have occurred to you: When a subclass object is created, whose constructor is executed
first, the one in the subclass or the one defined by the superclass? For example, given a
subclass called B and a superclass called A , is A 's constructor executed before B 's, or vice
versa? The answer is that in a class hierarchy, constructors complete their execution in or-
der of derivation, from superclass to subclass. Further, since super( ) must be the first state-
ment executed in a subclass' constructor, this order is the same whether or not super( ) is
used. If super( ) is not used, then the default (parameterless) constructor of each superclass
will be executed. The following program illustrates when constructors are executed:
Search WWH ::




Custom Search