9.2 Writing Constructors for Subclasses
Enduring Understanding
When multiple classes contain common attributes and behaviors, programmers create a new class containing the shared attributes and behaviors forming a hierarchy. Modifications made at the highest level of the hierarchy apply to the subclasses.
Learning Objective
Create an inheritance relationship from a subclass to the superclass.
Essential Knowledge
Constructors are not inherited.
The superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters.
The actual parameters passed in the call to the superclass constructor provide values that the constructor can use to initialize the object’s instance variables.
When a subclass’s constructor does not explicitly call a superclass’s constructor using super, Java inserts a call to the superclass’s no-argument constructor.
Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called. At this point, all of the constructors within the hierarchy execute beginning with the Object constructor.
Last updated
Was this helpful?