454 Object-Oriented Programming Chapter 9 Java automatically attempts (X web hosting)

454 Object-Oriented Programming Chapter 9 Java automatically attempts to call the superclass s default constructor. Note that lines 10 and 17 are comments indicating where the call to the superclass Object s default constructor occurs. Software Engineering Observation 9.6 Every subclass constructor must call one of the direct superclass constructors explicitly or implicitly. Implicit calls can be made only to the no-argument constructor of the superclass. If the superclass does not provide a no-argument constructor, all direct subclasses of that class must call one of superclass s constructors explicitly. Class Circle (Fig. 9.5) inherits from class Point as specified with the extends keyword on line 4. Keyword extendsin the class definition indicates inheritance. All the (nonprivate) members of class Point(except the constructors) are inherited into class Circle. Thus, the public interface to Circle includes the Point class s public methods as well as the two overloaded Circleconstructors and Circlemethods set- Radius, getRadius, area and toString. Notice that method area (lines 38 41) uses predefined constant Math.PI from class Math(package java.lang) to calculate the area of a circle. 1 // Fig. 9.5: 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 occurs here 11 setRadius( 0 ); 12 } 13 14 // constructor 15 public Circle( double circleRadius, int xCoordinate, 16 int yCoordinate ) 17 { 18 // call superclass constructor to set coordinates 19 super( xCoordinate, yCoordinate ); 20 21 // set radius 22 setRadius( circleRadius ); 23 } 24 25 // set radius of Circle 26 public void setRadius( double circleRadius ) 27 { 28 radius = ( circleRadius >= 0.0 ? circleRadius : 0.0 ); 29 } 30 Fig. 9.5 Circleclass definition (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply