<< Chapter < Page | Chapter >> Page > |
The interface named IVolume declares one method named volume . Likewise, the interface named ICircumference declares one method named circumference .
The rules of the road
Any number of classes can implement the same interface. This makes it possible to treat objects instantiated from different classes as the commoninterface type.
A class can implement any number of interfaces. In this program, however, the classes named MyRectangle and MyCircle each implement only one interface named IArea .
Can extend any number of interfaces
An interface can extend any number of other interfaces. In this program, the IArea interface extends both the IVolume interface and the ICircumference interface.
The bottom line
Implementing the IArea interface causes both the MyRectangle class and the MyCircle class to inherit the following abstract methods:
The first two methods are inherited directly from the interface named IArea . The last two are inherited from the interfaces named ICircumference and IVolume by way of IArea .
Each of the two classes must provide concrete definitions for all four of the inherited abstract methods.
Program output at startup
Figure 2 shows the program output at startup.
The program GUI
The program GUI consists of one label , three buttons and a text area . When a button is clicked, a random process is used to instantiate an object ofeither the MyCircle class or the MyRectangle class. The new object's reference is saved in a variable of the interface type IArea . This is possible because both classes implement the common IArea interface.
Call methods using the reference of type IArea
Then, as a result of a click on a button, the program calls the id method on the object whose reference is stored in the variable of type IArea . Depending on which button is clicked, the program then calls either the area method, the circumference method, or the volume method on the object.
Output after clicking the Area Button for circle
Clicking the button labeled Area, for example causes the output to change to something similar to either Figure 3 or Figure 4. An output similar toFigure 3 is produced if the random process instantiates an object of the MyCircle class.
Output after clicking the Area Button for rectangle
An output similar to Figure 4 is produced if the random process instantiates an object of the MyRectangle class.
Random radius, width, and height
I use the word similar because a random process is also used to establish the radius for the circle and to establish the width and height for the rectangle.
Both the MYCircle object and the MyRectangle object contain concrete versions of the four methods listed above . However, the behavior of those four methods in one object is different from the behavior of the four methods havingthe same names in the other object.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?