Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Inheritance > About writing subclasses in Flash > Overriding methods and properties | |||
When a subclass extends a superclass, the subclass inherits all of the superclass's methods and properties. One of the advantages of working with classes and extending classes is that it allows you not only to provide new functionality to an existing class but also to modify existing functionality. For example, consider the Widget class that you created in Example: Extending the Widget class. You could create a new method in your superclass (Widget) and then either override the method in your subclass (SubWidget) or just use the inherited method from the Widget class. The following example shows how you can override existing methods in your classes.
Note: If you created the Widget class in an earlier example, modify the existing code by adding the doSomething() method, as follows:
class Widget {
public static var widgetCount:Number = 0;
public function Widget() {
Widget.widgetCount++;
}
public function doSomething():Void {
trace("Widget::doSomething()");
}
}
The Widget class now defines a constructor and a public method called doSomething().
|
NOTE |
|
If you created the SubWidget class in Example: Extending the Widget class, you can use this file instead. |
class SubWidget extends Widget {
public function SubWidget() {
trace("Creating subwidget # " + Widget.widgetCount);
doSomething();
}
}
Notice that the SubWidget class's constructor calls the doSomething() method that you defined in the superclass.
var sw1:SubWidget = new SubWidget(); var sw2:SubWidget = new SubWidget();
Creating subwidget # 1 Widget::doSomething() Creating subwidget # 2 Widget::doSomething()
This output shows that the SubWidget class's constructor calls the constructor of its superclass (Widget), which increments the static widgetCount property. The SubWidget's constructor traces the superclass's static property and calls the doSomething() method, which inherits from the superclass.
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()");
}
}
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.
The Output panel displays the contents of the Widget class's doSomething() method.
Flash CS3