Chapter 9 Object-Oriented Programming (Cpanel web hosting) 489 Class Circle(Fig. 9.24)
Chapter 9 Object-Oriented Programming 489 Class Circle(Fig. 9.24) is derived from Point. A Circlehas a volume of 0.0, so superclass method volume is not overridden it is inherited from class Point, which inherited it from Shape. A Circle has an area different from that of a Point, so the area method is overridden. Method getName is an implementation of the abstract method in the superclass. If this method is not overridden here, the Pointversion of get- Name would be inherited. Other methods include setRadius to assign a new radius to a Circleand getRadiusto return the radiusof a Circle. 1 // Fig. 9.24: Circle.java 2 // Definition of class Circle 3 4 public class Circle extends Point { // inherits from Point 5 protected double radius; 6 7 // no-argument constructor 8 public Circle() 9 { 10 // implicit call to superclass constructor here 11 setRadius( 0 ); 12 } 13 14 // constructor 15 public Circle( double circleRadius, int xCoordinate, 16 int yCoordinate ) 17 { 18 // call superclass constructor 19 super( xCoordinate, yCoordinate ); 20 21 setRadius( circleRadius ); 22 } 23 24 // set radius of Circle 25 public void setRadius( double circleRadius ) 26 { 27 radius = ( circleRadius >= 0 ? circleRadius : 0 ); 28 } 29 30 // get radius of Circle 31 public double getRadius() 32 { 33 return radius; 34 } 35 36 // calculate area of Circle 37 public double area() 38 { 39 return Math.PI * radius * radius; 40 } 41 Fig. 9.24 Circlesubclass of Point indirect subclass of abstractclass Shape(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01