Managed web hosting - Chapter 9 Object-Oriented Programming 455 31 // get
Chapter 9 Object-Oriented Programming 455 31 // get radius of Circle 32 public double getRadius() 33 { 34 return radius; 35 } 36 37 // calculate area of Circle 38 public double area() 39 { 40 return Math.PI * radius * radius; 41 } 42 43 // convert the Circle to a String 44 public String toString() 45 { 46 return “Center = ” + “[” + x + “, ” + y + “]” + 47 “; Radius = ” + radius; 48 } 49 50 } // end class Circle Fig. 9.5 Circleclass definition (part 2 of 2). The Circle constructors (lines 8 12 and 15 23) must invoke a Point constructor to initialize the superclass portion of a Circle object (i.e., variables x and y inherited from Point). The default constructor at lines 8 12 does not call a Point constructor explicitly, so Java automatically calls class Point s default constructor (defined at line 8 of Fig. 9.4) that initializes superclass members xand yto zeros. If class Pointcontained only the constructor with arguments (i.e., did not provide a default constructor), a compiler error would occur. Line 19 in the body of the second Circleconstructor explicitly invokes the Point constructor (defined at line 15 of Fig. 9.4) by using the superclass constructor call syntax keyword super followed by a set of parentheses containing the arguments to the superclass constructor. In this case, the arguments are the values xCoordinateand yCoordinatethat are used by the Pointconstructor to initialize the superclass members xand y). The call to the superclass constructor must be the first line in the body of the subclass constructor. To call the superclass default constructor explicitly, use the statement super(); // explicit call to superclass default constructor Common Programming Error 9.2 It is a syntax error if a super call by a subclass to its superclass constructor is not the first statement in the subclass constructor. Common Programming Error 9.3 It is a syntax error if the arguments to a super call by a subclass to its superclass constructor do not match the parameters specified in one of the superclass constructor definitions. A subclass can redefine a superclass method by using the same signature; this is called overriding a superclass method. When that method is mentioned by name in the subclass, Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01