308 Inheritance
11. Open the SubWidget class and add a new method named doSomething(). Modify your
class so that it matches the following code (add the code that’s in boldface):
class SubWidget extends Widget {
public function SubWidget() {
trace("Creating subwidget # " + Widget.widgetCount);
doSomething();
}
public function doSomething():Void {
trace("SubWidget::doSomething()");
}
}
12.
Save your changes to the class file, and then open subwidgetTest.fla again.
13. Select Control > Test Movie to test the file. You see the following output in the
Output panel:
Creating subwidget # 1
SubWidget::doSomething()
Creating subwidget # 2
SubWidget::doSomething()
The previous output shows that the doSomething() method in the SubWidget class’s
constructor is calling the
doSomething() method in the current class instead of
the superclass.
Open the SubWidget class again, and modify the SubWidget class’s constructor to call the
superclass’s
doSomething() method (add the code that’s in boldface):
public function SubWidget() {
trace("Creating subwidget # " + Widget.widgetCount);
super.doSomething();
}
As demonstrated, you can add the super keyword to call the superclass’s doSomething()
method instead of the
doSomething() method in the current class. For additional
information on
super, see the super entry in the ActionScript 2.0 Language Reference.
14. Save the SubWidget class file with the modified constructor and select Control > Test
Movie to republish the Flash document.
The Output panel displays the contents of the Widget class’s
doSomething() method.
Using polymorphism in an application
Object-oriented programming lets you express differences between individual classes using a
technique called polymorphism, by which classes can override methods of their superclasses
and define specialized implementations of those methods.