Chapter 9 Object-Oriented Programming 495 1 // Fig.

Chapter 9 Object-Oriented Programming 495 1 // Fig. 9.27: Shape.java 2 // Definition of interface Shape 3 4 public interface Shape { 5 6 // calculate area 7 public abstract double area(); 8 9 // calculate volume 10 public abstract double volume(); 11 12 // return shape name 13 public abstract String getName(); 14 } Fig. 9.27 Point, circle, cylinder hierarchy with a Shapeinterface. 1 // Fig. 9.28: Point.java 2 // Definition of class Point 3 4 public class Point extends Object implements Shape { 5 protected int x, y; // coordinates of the Point 6 7 // no-argument constructor 8 public Point() 9 { 10 setPoint( 0, 0 ); 11 } 12 13 // constructor 14 public Point( int xCoordinate, int yCoordinate ) 15 { 16 setPoint( xCoordinate, yCoordinate ); 17 } 18 19 // Set x and y coordinates of Point 20 public void setPoint( int xCoordinate, int yCoordinate ) 21 { 22 x = xCoordinate; 23 y = yCoordinate; 24 } 25 26 // get x coordinate 27 public int getX() 28 { 29 return x; 30 } 31 Fig. 9.28 Pointimplementation of interface Shape(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply