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:
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.
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.
As mentioned earlier, the superclass can be used as the reference type for an object instantiated with a subclass.
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:
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:
- 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.
- 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:
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:
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
:
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.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Given the following class structure:
Which of the following objects can be created?
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