7.4. What is Inherited?

In Java, when one class extends another, the child class inherits almost everything from the parent class. Specifically, a child class inherits instance variables and methods, regardless of their declared visibility, from its parent. These members are accessible, depending on their visibility, within the child class in much the same way they would be accessible in the parent class.

Note

Private instance variables are inherited. However, we should recall that they cannot be directly accessed (without using a getter/setter method) outside of the class in which they are declared.

7.4.1. The Object Class

In Java, the Object class (java.lang.Object) is the superclass for all other classes. In other words, every Java class automatically extends java.lang.Object without you having to explicitly add the extends keyword to the class declaration. Even classes you created in your previous programming class had this relationship with the Object class.

Take a few moments to look through the Object class documentation ([OBJECT]). Some methods may look strange to you, but note any methods you have seen before. Do you recognize toString? How about equals? Remember, any class that you have created automatically inherited those methods.

Important

If a class does not explicitly extend another class, then it implicitly extends Object. Therefore, Object is at the top of all inheritance hierarchies in Java.

7.4.2. Constructors

Constructors are not inherited in the usual sense. That is, a parent constructor does not become a constructor in the child class when inheritance is involved. However, child constructors can (and should) call the parent’s constructor using the super keyword on the first line of the child constructor.

When we say that a parent constructor should be called, we don’t mean the child constructor should create an object of the parent class. In other words, new Parent() should not be executed from within the child class. Instead, we are calling the parent constructor using super. Remember, constructors initialize objects - they don’t create them. So, when we call a constructor, we are simply allowing that constructor to initialize the instance variables declared in its class. We will illustrate this with a small example in the next section.

Important

“Using super to call a parent constructor” just means that we’re telling Java to execute the code that’s in a parent constructor.

Java does this in order to facilitate a separation of concerns; that is, it lets each class setup/initialize their own variables so that the child class doesn’t have to duplicate the initialization code that’s written in the parent.

If the author of a child class constructor does not explicitly call super, then Java will automatically add super() (i.e., a call to the parent’s default constructor) to the first line of the child constructor during compile-time. If a default (no argument) constructor doesn’t exist, a syntax error will occur. That’s why we say you should always call a parent constructor explicitly when creating a child class.

Pro Tip

It’s tempting to imagine that Java jumps back and forth from code in the child class to code in the parent class whenever super is used or when inherited methods are called; however, thinking of it like that is error prone and often confusing.

Instead, embrace the “copy-paste” ideology that’s espoused in our videos and apply that thinking here. Although the parent constructor is not included as a separate constructor in the child class, it’s convenient to think of it as being pasted into the child class (renamed to super) along with other inherited members. Instead of moving back and forth from child to parent, you simply “copy” the constructor into the child class and rename it. Eliminating the back and forth makes it easier to reason out what is happening.

Rapid Fire Review
  1. Which of the following statements is true about inheritance in Java?

    1. Child classes do not inherit private instance variables from parent classes.

    2. Methods with private visibility in the parent class are directly accessible in the child class.

    3. Child classes inherit instance variables and methods from their parent class.

    4. Constructors are automatically inherited by child classes.

  2. What is the superclass of all classes in Java?

    1. java.lang.String

    2. java.lang.Object

    3. java.lang.Integer

    4. java.util.List

  3. When a child class constructor calls the parent class constructor, which keyword is used?

    1. this

    2. super

    3. parent

    4. init

  4. What happens if a child class constructor does not explicitly call a parent constructor?

    1. Java will throw an error during compile-time.

    2. The child constructor will not be compiled.

    3. Java will automatically insert a call to the parent’s default constructor which may lead to an error during compile-time.

    4. The parent constructor will never be called.

  5. Which of the following is NOT inherited by a child class in Java?

    1. Methods from the parent class

    2. Private instance variables

    3. Public instance variables

    4. Parent class constructors