472 Object-Oriented Programming Chapter 9 27 28 //
472 Object-Oriented Programming Chapter 9 27 28 // set coordinates, radius and height 29 cylinder.setHeight( 10 ); 30 cylinder.setRadius( 4.25 ); 31 cylinder.setPoint( 2, 2 ); 32 33 // get String representation of Cylinder and calculate 34 // area and volume 35 output += “nnThe new location, radius ” + 36 “and height of cylinder aren” + cylinder + 37 “nArea is ” + precision2.format( cylinder.area() ) + 38 “nVolume is ” + precision2.format( cylinder.volume() ); 39 40 JOptionPane.showMessageDialog( null, output, 41 “Demonstrating Class Cylinder”, 42 JOptionPane.INFORMATION_MESSAGE ); 43 44 System.exit( 0 ); 45 } 46 47 } // end class Test Fig. 9.15 Testing class Cylinder(part 2 of 2). The series of examples in this section nicely demonstrates inheritance and defining and referencing protectedinstance variables. The reader should now be confident with the basics of inheritance. In the next several sections, we show how to program with inheritance hierarchies in a general manner, using polymorphism. Data abstraction, inheritance and polymorphism are the crux of object-oriented programming. 9.10 Introduction to Polymorphism With polymorphism, it is possible to design and implement systems that are more easily extensible. Programs can be written to process generically as superclass objects objects of all existing classes in a hierarchy. Classes that do not exist during program development can be added with little or no modifications to the generic part of the program as long as those classes are part of the hierarchy that is being processed generically. The only parts of Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01