Free php web host - 496 Object-Oriented Programming Chapter 9 32 // get
496 Object-Oriented Programming Chapter 9 32 // get y coordinate 33 public int getY() 34 { 35 return y; 36 } 37 38 // convert point into String representation 39 public String toString() 40 { 41 return “[” + x + “, ” + y + “]”; 42 } 43 44 // calculate area 45 public double area() 46 { 47 return 0.0; 48 } 49 50 // calculate volume 51 public double volume() 52 { 53 return 0.0; 54 } 55 56 // return shape name 57 public String getName() 58 { 59 return “Point”; 60 } 61 62 } // end class Point Fig. 9.28 Pointimplementation of interface Shape(part 2 of 2). When a class implements an interface, the same is a relationship provided by inheritance applies. For example, class Point implements Shape. Therefore, a Point object is a Shape. In fact, objects of any class that extends Pointare also Shapeobjects. Using this relationship, we have maintained the original definitions of class Circle, class Cylinder and application class Testfrom Section 9.18 (repoeated in Fig. 9.29 Fig. 9.31) to illustrate that an interface can be used instead of an abstractclass to process Shapes polymorphically. Notice that the output for the program (Fig. 9.31) is identical to Fig. 9.22. Also, notice that Objectmethod toStringis called through a Shapeinterface reference (line 44). Software Engineering Observation 9.31 All methods of class Object can be called by using a reference of an interface data type a reference refers to an object, and all objects have the methods defined by class Object. One benefit of using interfaces is that a class can implement as many interfaces as it needs in addition to extending a class. To implement more than one interface, simply provide a comma-separated list of interface names after keyword implements in the class definition. This is particularly useful in the GUI event-handling mechanism. A class that implements more than one event-listener interface (such as ActionListenerin earlier Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01