<< Chapter < Page | Chapter >> Page > |
ActionScript supports the notion of overriding a method inherited from a superclass to cause the named method to behave differently when called onobjects of different subclasses, each of which extends the same superclass and overrides the same method name.
Example - compute the area of different geometrical shapes
For example, consider the computation of the area of a geometrical shape in a situation where the type of geometrical shape is not known when the program iscompiled. Polymorphism is a tool that can be used to handle this situation.
Circle and Rectangle extend Shape
Assume that classes named Circle and Rectangle each extend a class named Shape . Assume that the Shape class defines a method named area . Assume further that the area method is properly overridden in the Circle and Rectangle classes to return the correct area for a circle or a rectangle respectively.
Three types of objects
In this case, a Circle object is a Shape object because the Circle class extends the Shape class. Similarly, a Rectangle object is also a Shape object.
Therefore, an object of the Shape class, the Circle class, or the Rectangle class can be instantiated and any one of the three can be saved in a variable of type Shape .
Flip a virtual random coin
Assume that the program flips a virtual coin and, depending on the outcome of the flip, instantiates an object of either the Circle class or the Rectangle class and saves it in a variable of type Shape . Assuming that the coin flip is truly random, the compiler cannot possibly knowat compile time which type of object will be stored in the variable at runtime.
Two versions of the area method
Regardless of which type of object is stored in the variable, the object will contain two versions of the method named area . One version is the version that is defined in the Shape class, and this version will be the same regardless of whether the object is a circle or a rectangle.
Also, this version can't return a valid area because a general shape doesn't have a valid area. However, if the area method is defined to return avalue, even this version must return a value even if it isn't valid. (Other programming languages get around this problem with something called an abstract class, which isn't allowed in ActionScript 3.)
The other version of the area method will be different for a Circle object and a Rectangle object due simply to the fact the algorithm for computing the area of a circle is different from the algorithm forcomputing the area of a rectangle.
Call the area method on the object
If the program calls the area method on the object stored in the variable of type Shape at runtime, the correct version of the area method will be selected and executed and will return the correct area for the type of object stored in that variable. This is runtime polymorphism based on method overriding.
A more general description of runtime polymorphism
A reference variable of a superclass type can be used to reference an object instantiated from any subclass of the superclass.
If an overridden method in a subclass object is called using a superclass-type reference variable, the system will determine, at runtime, whichversion of the method to use based on the true type of the object, and not based on the type of reference variable used to call the method.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?