ArrayList superclass java
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 hierarchy. Every class in Java inherits from Object and is-an Object.
One of the main reasons to use an 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.
9-5-1: What variables and methods might be inherited from the superclass Shape in the inheritance hierarchy above?
9-5-2: Can you make a 3 level inheritance hierarchy for living things on Earth?
A superclass reference variable can hold an object of that superclass or of any of its subclasses. For example, a Shape reference variable can hold a Rectangle or Square object. (This is a type of polymorphism which will be defined in the next lesson). // The variables declared of type Shape can hold objects of its subclasses Shape s1 = new Shape(); Shape s2 = new Rectangle(); Shape s3 = new Square(); 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 to Square” error (although you could use a type-cast to get it to be a (Square)). // A subclass variable cannot hold the superclass object! // A Square is-a Shape, but not all Shapes are Squares. // Square q = new Shape(); // ERROR!! 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 as seen in the next sections. Check your understanding
9-5-3: A class Student inherits from the superclass Person. Which of the following assignment statements will give a compiler error?
Another advantage of an inheritance hierarchy is that we can write methods with parameters of the superclass type and pass in subclass objects to them. For example, the print(Shape) method below could be called with many different Shape subclasses and work for Rectangles, Squares, etc. // This will work with all Shape subclasses (Squares, Rectangles, etc.) too public void print(Shape s) { ... } Coding ExerciseNotice 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? It depends on whether a Person or Student is passed in at runtime. What would happen if you commented out the Student toString() method? Which one would be called now?
Which toString() method is called below? What would happen if you commented out the Student toString() method? Which one would be called now?
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. // This shape array can hold the subclass objects too Shape[] shapeArray = { new Rectangle(), new Square(), new Shape() }; // The shape ArrayList can add subclass objects too ArrayList<Shape> shapeList = new ArrayList<Shape>(); shapeList.add(new Shape()); shapeList.add(new Rectangle()); shapeList.add(new Square()); 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! Coding ExerciseThe 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!
Scroll down to look at the Dog class and add a similar Cat class that extends Pet. Don’t make the Cat class public because there can only be 1 public class in a file. Scroll back to the main method and add some Cat objects to the ArrayList too. Does the petList work with Cats too? import java.util.*; // for ArrayList public class Pet { private String name; private String type; public Pet(String n, String t) { name = n; type = t; } public String toString() { return name + " is a " + type; } public static void main(String[] args) { ArrayList
9.5.5. Summary¶
|