Imagine that you have a class hierarchy of shapes:
Shape:
Polygon
Triangle
Quadrilateral
Parallelogram
Rectangle
Square
Rhombuse
Trapezoid
Circle
Imagine that you have an applet that declares an array of Shape:
Shape arrayOfShapes[] = new Shape[100];
Imagine that you have a method that iterates through the array, 1 element at a time, and draws each shape
encountered by invoking a draw method that exists in the Shape class, but is overridden in each of its subclasses.
public void paint(Graphics g) {
for (int i = 0; i < arrayOfShapes.length; i++)
arrayOfShapes[i].draw(g);
}
Imagine that if you add a new subclass of Triangle, say Scalene (with its associated draw method), your draw
method in Shape does not have to change at all. Your applet just works!