Chapter 9 Object-Oriented Programming 453 1 // Fig. (Geocities web hosting)
Chapter 9 Object-Oriented Programming 453 1 // Fig. 9.4: Point.java 2 // Definition of class Point 3 4 public class Point { 5 protected int x, y; // coordinates of Point 6 7 // No-argument constructor 8 public Point() 9 { 10 // implicit call to superclass constructor occurs here 11 setPoint( 0, 0 ); 12 } 13 14 // constructor 15 public Point( int xCoordinate, int yCoordinate ) 16 { 17 // implicit call to superclass constructor occurs here 18 setPoint( xCoordinate, yCoordinate ); 19 } 20 21 // set x and y coordinates of Point 22 public void setPoint( int xCoordinate, int yCoordinate ) 23 { 24 x = xCoordinate; 25 y = yCoordinate; 26 } 27 28 // get x coordinate 29 public int getX() 30 { 31 return x; 32 } 33 34 // get y coordinate 35 public int getY() 36 { 37 return y; 38 } 39 40 // convert into a String representation 41 public String toString() 42 { 43 return “[” + x + “, ” + y + “]”; 44 } 45 46 } // end class Point Fig. 9.4 Pointclass definition. Class Point s constructors (lines 8 12 and 15 19) must call class Object s constructor. In fact, every subclass constructor is required to call its direct superclass s constructor as its first task, either implicitly or explicitly (the syntax for this call is discussed with class Circlemomentarily). If there is no explicit call to the superclass constructor, Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01