10.2. Inheritance and Constructors

Subclasses inherit all the private instance variables in a superclass that they extend, but they cannot directly access them since they are private. And constructors are not inherited.

How do you initialize inherited private variables if you don’t have direct access to them in the subclass? In Java you can put a call to the parent constructor using as the first line in a subclass constructor. In Java, the superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters, for example super(); or super(theName); as in the code below. The actual parameters given to super() are used to initialize the inherited instance variables, for example the name instance variable in the Person superclass.

public class Employee extends Person
{
    public Employee()
    {
        super(); // calls the Person() constructor
    }
    public Employee(String theName)
    {
        super(theName); // calls Person(theName) constructor
    }
}

coding exercise Coding Exercise

The super(theName) in the Employee constructor will call the constructor that takes a String object in the Person class to set the name.

Try creating another Employee object in the main method that passes in your name and then use the get methods to print it out. Which class constructor sets the name? Which class constructor sets the id?

If a class has no constructor in Java, the compiler will add a no-argument constructor. A no-argument constructor is one that doesn’t have any parameters, for example public Person().

If a subclass has no call to a superclass constructor using super as the first line in a subclass constructor then the compiler will automatically add a super() call as the first line in a constructor. So, be sure to provide no-argument constructors in parent classes or be sure to use an explicit call to super() as the first line in the constructors of subclasses.

Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called since every class inherits from the Object class.

exercise Check your understanding

You can step through this code using the Java Visualizer by clicking the following link Named Point.

10.2.1. groupwork Programming Challenge : Square is-a Rectangle

In this challenge, you are giving a class called Rectangle that has two instance variables, length and width, and two constructors that initialize them, and a method called draw() that uses nested loops to draw a length x width rectangle of stars. Try it out below.

You will write a new class called Square that inherits from Rectangle. Is a square a rectangle? Yes! A square is a rectangle where the length and width are equal. Square will inherit length, width, and the draw method. You will write square constructors that will call the Rectangle constructors.

  1. Make the class Square below inherit from Rectangle

  2. Add a Square no-argument constructor that calls Rectangle’s constructor using super().

  3. Add a Square constructor with 1 argument for a side that calls Rectangle’s constructor with 2 arguments using super.

  4. Uncomment the objects in the main method to test drawing the squares.

  5. Add an area() method to Rectangle that computes the area of the rectangle. Does it work for squares too? Test it.

  6. Add another subclass called LongRectangle which inherits from Rectangle but has the additional condition that the length is always 2 x the width. Write constructors for it and test it out.

Create a Square class that inherits from Rectangle.

For a more complex example of drawing shapes, try running this repl.it Java Swing code (or download the files here by clicking on Download on the top right and use the files in your own Java IDE). When the yellow panel comes up, click on either the Rectangle or the Oval button and then click and drag somewhere on the yellow panel to draw that shape. Take a look at the Rectangle.java and Oval.java files to see how they inherit from the Shape class in Shape.java. Java Swing graphical programming is not covered on the AP CS A exam, but it is a lot of fun!

10.2.2. Summary

  • Subclasses inherit all the private instance variables in a superclass that they extend, but they cannot directly access them since they are private.

  • 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 to set the private instance variables of the superclass.

  • 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.

You have attempted of activities on this page