Shared web hosting - Chapter 9 Object-Oriented Programming 493 54 55 System.exit(
Chapter 9 Object-Oriented Programming 493 54 55 System.exit( 0 ); 56 } 57 58 } // end class Test Fig. 9.26 Shape, Point, Circle, Cylinderhierarchy (part 2 of 2). Lines 33 through 36 invoke methods getNameand toString to illustrate that the objects are initialized correctly (as in the first three lines of the screen capture). Next, the forstructure at lines 42 49 walks through arrayOfShapes, and the following calls are made during each iteration of the loop: arrayOfShapes[ i ].getName() arrayOfShapes[ i ].toString() arrayOfShapes[ i ].area() arrayOfShapes[ i ].volume() Each of these method calls is invoked on the object to which arrayOfShapes[i] currently refers. When the compiler looks at each of these calls, it is simply trying to determine whether a Shapereference (arrayOfShapes[ i]) can be used to call these methods. For methods getName, areaand volumethe answer is yes, because each of these methods is defined in class Shape. For method toString, the compiler first looks at class Shapeto determine that toStringis not defined there, then the compiler proceeds to Shape s superclass (Object) to determine whether Shape inherited a toString method that takes no arguments (as it did, because all Objects have a toStringmethod). The screen capture illustrates that all four methods are invoked properly based on the type of the referenced object. First, the string “Point:”and the coordinates of the object point (arrayOfShapes[0]) are output; the area and volume are both 0. Next, the string “Circle:”, the coordinates of object circle, and the radius of object circle (arrayOfShapes[1]) are output; the area of circleis calculated and the volume is Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01