I.5: CREATING REFERENCES USING INHERITANCE HIERARCHIES
OBJECTIVE
I can:
define reference variables of a superclass to be assigned to an object of a subclass in the same hierarchy
If you have multiple subclasses that inherit from a superclass, you can form an inheritance hierarchy.
Every subclass is-a or is a kind of the superclass.
For example, here is an inheritance hierarchy of Shapes.
Square is-a Rectangle and a subclass of Rectangle.
Rectangle is-a Shape and a subclass of Shape.
In Java, the class Object is at the top of the hierarchy.
Every class in Java inherits from Object and is-an Object.
One of the main reasons to use and inheritance hierarchy is that the instance variables and methods from a superclass are inherited and can be used in a subclass without rewriting or copying code.
SUPERCLASS REFERENCES
A superclass reference variable can hold an object of that superclass or and of its subclasses.
For example, a Shape reference variable can hold a Rectangle or Square object.
Notice that the opposite is not true.
You cannot declare a variable of the subclass and put in a superclass object.
For example, a Square reference cannot hold a Shape object because not all Shapes are Squares.
The code below will give an "incompatible types: Shape cannot be converted into Square" error (although you could use a type-cast to get it to be a (Square)).
Why is using a superclass reference for subclass objects useful?
Because now, we can write methods with parameters of type Shape or have arrays of type Shape and use them with any of its subclasses.
SUPERCLASS METHOD PARAMETERS
Another advantage of an inheritance hierarchy is that we can write methods with parameters of the superclass and pass in subclass objects to them.
For example, the print(Shape) method could be called with many different Shape subclasses and work for Rectangles, Squares, etc.
Notice that in the following code, the print method has a parameter of type Person, but it can be called with Student or Person objects in the main method.
Which toString() method is called?
Notice that I added + " (Person)" and + " (Student)" to the toString() methods to make it clear which method is being called when!
It depends on whether a Person or Student is passed in at runtime.
What would happen if you commented out the Student toString() method?
Notice that only the Person toString() method is called:
SUPERCLASS ARRAYS AND ARRAYLISTS
Using Inheritance hierarchies, we can create arrays and ArrayLists using the superclass type and put in values that are of the subclass type.
This can be very useful!
For example, here is a Shape array and a Shape ArrayList that can hold any objects of the Shape subclasses:
Notice that the add method in ArrayLists actually has a parameter type of Object, add(Object), but we can use it with any subclass object!
The code below has an ArrayList of Pets that can hold Pet or Dog objects.
Notice that the loop works with a variable of type Pet because Dogs are Pets too!
I have two cats (Vash and Fey) so I added a similar Cat class that extends Pet.
Notice that I added two Cat objects to the ArrayList in the main method below.
The petList works for cats too!
SUMMARY
An inheritance hierarchy of subclasses inheriting from superclasses can be formed with Object being the top of the hierarchy.
When a class S "is-a" class T, T is referred to as a superclass, and S is referred to as a subclass.
If S is a subclass of T, then a reference of type T can be used to refer to an object of type T or S. This is called polymorphism, defined more in the next lesson.
Declaring references of type T, when S is a subclass of T, is useful in the declaring formal method parameters of type T, arrays of type T [], and ArrayList<T> of type T so that all the subclasses of T can also be used with these.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) The following code contains a class called ShoppingCart that simulates a grocery store or an online store's shopping cart. It has an ArrayList called order that you can use to add items to the shopping cart. The item class keeps track of the name and the price of each item. If you run the code, you will see that it adds 2 items to the cart and then prints out the total order.
In this challenge, add a new class called DiscountedItem that extends the Item class. The ArrayList of items will still work since it can hold the subclasses of items too! The ShoppingCart printOrder() method will work with Items and DiscountedItems but note that it has an if-statement that treats DiscountedItems differently.
In the DiscountedItem subclass:
a. Add an instance variable for the discount amount.
b. Add constructors that call the super constructor item.
c. Add get/set methods for discount. The get method is given in the program but you should modify it!
d. Add a toString() method that returns a String that includes a call to the super toString() method that will print out the price as well as the discount amount using the super.valueToString() method to format it. You should put the discount in the parentheses with a minus sign in the front of it like this "(- $0.50)".
e. Uncomment the code in the main method to test adding DiscountedItems to the cart.