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.

To override methods in a subclass:

  1. Create a new ActionScript document and save it as Widget.as.
  2. In Widget.as, type the following ActionScript code into the Script window.

    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()");
        }
    }
    
  3. Save your changes to the ActionScript document.

    The Widget class now defines a constructor and a public method called doSomething().

  4. Create a new ActionScript file named SubWidget.as and save it in the same directory as Widget.as.

    NOTE

     

    If you created the SubWidget class in Example: Extending the Widget class, you can use this file instead.

  5. In SubWidget.as, type the following ActionScript code into the Script window:
    class SubWidget extends Widget {
        public function SubWidget() {
            trace("Creating subwidget # " + Widget.widgetCount);
            doSomething();
        }
    }
    
  6. Save your changes to SubWidget.as.

    Notice that the SubWidget class's constructor calls the doSomething() method that you defined in the superclass.

  7. Create a new Flash document and save it as subWidgetTest.fla in the same directory as the ActionScript documents.
  8. In subWidgetTest.fla, type the following ActionScript into Frame 1 of the main Timeline:
    var sw1:SubWidget = new SubWidget();
    var sw2:SubWidget = new SubWidget();
    
  9. Save your changes to the Flash document.
  10. Select Control > Test Movie to test the Flash document. You see the following output in the Output panel:
    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.

  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.


Flash CS3