494 Object-Oriented Programming Chapter (Disney web site) 9 0. Finally, the

494 Object-Oriented Programming Chapter 9 0. Finally, the string “Cylinder:”, the coordinates of object cylinder, the radius of object cylinder and the height of object cylinder (arrayOfShapes[2]) are output; the area of cylinder is calculated and the volume of cylinder is calculated. All the method calls to getName, toString, area and volume are resolved at runtime with dynamic binding. 9.19 Case Study: Creating and Using Interfaces Our next example (Fig. 9.27 Fig. 9.31) reexamines the Point, Circle, Cylinderhierarchy one last time, replacing abstract superclass Shape with the interface Shape (Fig. 9.27). An interface definition begins with the keyword interface and contains a set of publicabstract methods. Interfaces may also contain publicstaticfinal data. To use an interface, a class must specify that it implements the interface and the class must define every method in the interface with the number of arguments and the return type specified in the interface definition. If the class leaves any method of the interface undefined, the class becomes an abstract class and must be declared abstract in the first line of its class definition. Implementing a interface is like signing a contract with the compiler that states, I will define all the methods specified by the interface. Common Programming Error 9.9 Leaving a method of an interface undefined in a class that implements the interface results in a compile error indicating that the class must be declared abstract. Software Engineering Observation 9.30 Declaring a final reference indicates that the reference always refers to the same object. However, this does not affect the object to which the reference refers the object s data still can be modified. We started using the concept of an interface when we introduced GUI event handling in Chapter 6, Methods. Recall that our applet class included implements Action- Listener (an interface in package java.awt.event). The reason we were required to define actionPerformed in the applets with event handling is that ActionListener is an interface that specifies that actionPerformed must be defined. Interfaces are an important part of GUI event handling, as we will discuss in the next section. An interface is typically used in place of an abstract class when there is no default implementation to inherit i.e., no instance variables and no default method implementations. Like publicabstract classes, interfaces are typically public data types, so they are normally defined in files by themselves with the same name as the interface and the .java extension. The definition of interface Shape begins in Fig. 9.27 at line 4. Interface Shape has abstract methods area, volume and getName. By coincidence, all three methods take no arguments. However, this is not a requirement of methods in an interface. In Fig. 9.28, line 4 indicates that class Point extends class Object and implements interface Shape. Class Point provides definitions of all three methods in the interface. Method area is defined at lines 45 48. Method volume is defined at lines 51 54. Method getName is defined at lines 57 60. These three methods satisfy the implementation requirement for the three methods defined in the interface. We have fulfilled our contract with the compiler. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply