9.2 Writing Constructors for Subclasses
Class hierarchies are used to put common attributes and behaviors of related classes into a single class called a superclass. This enables the sharing and reusing of code in any number of subclasses. Unfortunately, subclasses don’t inherit the constructor from the superclass, so they need their own, either explicitly created or a default constructor created by Java.
In order to use a superclass, the subclass constructor must call the parent constructor. When a subclass is created, a superclass object is also created. The subclass must make a call to the superclass constructor when it exists.
The keyword super
is always used on the first line of our subclass constructor, which is passed the parameters needed to satisfy the superclass constructor:
In this example, the Student
constructor takes name
, birthday
, grade
, and gpa
. The grade
and gpa
values are set in the Student
object, but name
and birthday
get passed to the Person
class. Using the super
keyword, the name
and birthday
are taken from the Student
class and passed directly to the Person
class. Inside the Person class
, these parameters are used to initialize the object’s instance variables.
If a subclass doesn’t explicitly call a superclass constructor using the keyword super
, Java inserts a call to the superclass’s no-argument constructor. If a no-argument constructor doesn’t exist, then the program will not compile. In other words, one way or another, your subclass needs to be able to correctly create a superclass object.
When a class doesn’t explicitly extend another class, then by default it is extending the Object
class. The object class is at the top of the Java class hierarchy and contains several methods that all objects can use, such as the toString()
method. The Object
class constructor doesn’t take any parameters, so it does not explicitly need to be called.
Each subclass will either implicitly or explicitly call the superclass constructor. This process continues as the constructors move up the hierarchy and only completes once the Object
constructor has been called. At this point, all of the constructors within the hierarchy execute beginning with the Object
constructor.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
True or False: A subclass can NOT be extended by another class to become a superclass.
In this problem, you’ll design and test a few classes that represent different pieces of clothing in a clothing store.
You’ll write the classes for TShirt
, Jeans
, Sweatshirt
and Clothing
.
The Clothing
class should have two instance variables: one for the size of the clothing (a String), and another for the clothing’s color (also a string).
Clothing should have two accessors (getter methods) as well:
The Sweatshirt
class should have a private instance variable (or field) to store whether or not it has a hood and a corresponding getter method
The TShirt
class should have a private field to store the fabric and a corresponding getter for that called:
All Jean
s should have the color blue.
The constructors should be of this format:
Be sure and test your methods in the ClothingTester
class by creating different objects and printing out the results.