Please enable JavaScript to use CodeHS

Chapter 9

Inheritance

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.

super Keyword

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:

public class Person {
   private String name;
   private String birthday;

   public Person (String name, String birthday){
      this.name = name;
      this.birthday = birthday;
   }

   public String getName(){
      return name;
   }
}

public class Student extends Person {
   private int grade;
   private double gpa;

   public Student (String name, String birthday, int grade, double gpa) {
      super(name, birthday);
      this.grade = grade;
      this.gpa = gpa;
   }

   public int getGrade(){
      return grade;
   }
}
Plain text

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.

The Object Class

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.

Student Subclass

Shape Class

Implicit Call to Super

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    True or False: A subclass can NOT be extended by another class to become a superclass.

Exercise: Clothing Store

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:

public String getSize() { ... }
public String getColor() { ... }
Plain text

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

public boolean hasHood() { ... }
Plain text

The TShirt class should have a private field to store the fabric and a corresponding getter for that called:

public String getFabric() { ... }
Plain text

All Jeans should have the color blue.

The constructors should be of this format:

public Clothing(String size, String color) { ... }

public TShirt(String size, String color, String fabric) { ... }

public Sweatshirt(String size, String color, boolean hasHood) { ... }

public Jeans(String size) { ... }
Plain text

Be sure and test your methods in the ClothingTester class by creating different objects and printing out the results.