Please enable JavaScript to use CodeHS

Chapter 9

Inheritance

9.5 Creating References Using Inheritance Hierarchies

Up to this point, every object declaration and initialization that you’ve written has created objects where the Reference type matched the Object type:

While this is often the way that objects should be written, the object type and reference type can be different. The Reference type of an object variable can be declared with a superclass, then instantiated with a subclass:

Person student1 = new Student();
Plain text

Doing so facilitates polymorphism. Polymorphism is the capability of a method to do different things depending on the object it is acting upon. In Java and other object-oriented programming languages, the concept of polymorphism means that an object can take on different forms depending on its implementation. Java can call the correct method even when an object is disguised as a more generic reference type.

Java Documentations & Class Hierarchy

In standard Java Documentation, superclasses are denoted with the letter T, while subclasses are denoted with the letter S. If class S is a class T, then S is a subclass to the T superclass.

This terminology will be used throughout this chapter to refer to the relationship between superclasses and subclasses.

Superclass as a Reference Type

As mentioned earlier, the superclass can be used as the reference type for an object instantiated with a subclass.

Person student1 = new Student();
Plain text

More broadly, this means that when the Superclass is used as a reference type, then an object can be created as either the Superclass T, or any Subclass S. For example, the Person reference type can be used when instantiating an object as a Person, Student, or Worker, so long as the instantiated type is a subclass of the Person class:

Person student1 = new Person();
Person student1 = new Worker();
Person student1 = new Student();
Plain text

Applications of Polymorphism

The ability to declare an object with the superclass Reference type enables the use of polymorphism, where methods are able to change their behavior. This is most useful and evident in the following ways:

  1. It enables the use of type T as a formal parameter in a method, which can then pass any object of type T or S.
  2. It enables the creation of arrays and Arraylist of type T that stores any type T or S objects.

For the former, consider the following method:

public static void greeting(Person person)
{
    System.out.println("Hello " + person.getName());
}
Plain text

The formal parameter in this method specifies that the object type that is being passed is of type Person. A Student object can still be passed to this method if it is declared with the Person reference type:

Person karel = new Student("Karel");
greeting(karel); //prints 'Hello Karel'
Plain text

This same concept applies to storing values in an array or ArrayList. If an array is declared with type Person, then Student objects are still able to be added when declared as type Person:

Person karel = new Student("Karel");
Person jeremy = new Person("Jeremy");
Person[] people = {karel, jeremy};
Plain text

This process illustrates how polymorphism can be used to broaden the use of methods and data structures to include objects of multiple classes within a hierarchy.

Animal Sounds

Shape Areas

Person Class

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    Given the following class structure:

    public class Vehicle {
       /* implementation not shown */
    }
    
    public class Truck extends Vehicle {
       /* implementation not shown */
    }
    Java

    Which of the following objects can be created?

    Vehicle obj1 = new Vehicle();
    Vehicle obj2 = new Truck();
    Truck obj3 = new Truck();
    Truck obj4 = new Vehicle();
    Java

Exercise: Creating .equals()

For this exercise, we are going to look at how we can create a .equals() method. Remember, to compare objects, we cannot just use ==. We need to create criteria to determine if the objects are equal.

In our case, we will say that events are the same if both have the same event name and venue name. We are going to write a program that prompts the user for an Event name and venue and a Concert name, venue, and artist name, and decide if they are the same event.

Start in the Event class creating the .equals() method. Remember, this should take the other Event it wants to compare to as an argument and return a boolean if both the name and venue are equal.

Once completed, create the tester class where you will prompt the user for information. Sample output is provided below.

Sample Output

Please enter the Event name: Super Bowl XLVII 
Please enter the Event venue: Mercedes-Benz Superdome
Please enter the other Concert name: Super Bowl XLVII 
Please enter the Concert venue: Mercedes-Benz Superdome
Please enter the Concert artist: Beyonce
Same: true
Plain text
Please enter the Event name: Atlanta United FC
Please enter the Event venue: Mercedes-Benz Superdome
Please enter the Concert name: Super Bowl XLVII 
Please enter the Concert venue: Mercedes-Benz Superdome
Please enter the Concert artist: Beyonce
Same: false
Plain text