Net web server - 490 Object-Oriented Programming Chapter 9 42 // convert

490 Object-Oriented Programming Chapter 9 42 // convert Circle to a String represention 43 public String toString() 44 { 45 return “Center = ” + super.toString() + 46 “; Radius = ” + radius; 47 } 48 49 // return shape name 50 public String getName() 51 { 52 return “Circle”; 53 } 54 55 } // end class Circle Fig. 9.24 Circlesubclass of Point indirect subclass of abstractclass Shape(part 2 of 2). Software Engineering Observation 9.29 A subclass always inherits the most recently defined version of each public and protected method from its direct and indirect superclasses. Class Cylinder (Fig. 9.25) is derived from Circle. A Cylinder has area and volume different from those of class Circle, so the areaand volumemethods are both overridden. Method getName is an implementation of the abstract method in the superclass. If this method had not been overridden here, the Circleversion of getName would be inherited. Other methods include setHeight to assign a new height to a Cylinderand getHeightto return the heightof a Cylinder. 1 // Fig. 9.25: Cylinder.java 2 // Definition of class Cylinder. 3 4 public class Cylinder extends Circle { 5 protected double height; // height of Cylinder 6 7 // no-argument constructor 8 public Cylinder() 9 { 10 // implicit call to superclass constructor here 11 setHeight( 0 ); 12 } 13 14 // constructor 15 public Cylinder( double cylinderHeight, 16 double cylinderRadius, int xCoordinate, 17 int yCoordinate ) 18 { 19 // call superclass constructor 20 super( cylinderRadius, xCoordinate, yCoordinate ); Fig. 9.25 Cylindersubclass of Circle indirect subclass of abstract class Shape(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply