Adobe Flex 3 Help

Defining a method override

You can override a method of a base class in your ActionScript component. To override the method, you add a method with the same signature to your class, and prefix it with the override keyword. The following example overrides the HBox.addChild() method to open an Alert box when a new item is added to it:

package myComponents
{
    import mx.controls.Alert;
    import mx.containers.HBox;
    import flash.display.DisplayObject;

    public class HBoxWithAlert extends HBox
    {
        // Define the constructor.  
        public function HBoxWithAlert() 
        {
            super();
        }       

      // Define the override.
      override public function addChild(child:DisplayObject):DisplayObject {
        
            // Call super.addChild().
            super.addChild(child); 
            
            // Open the Alert box.
            Alert.show("Item added successfully");

            return child;
        }
    }
}

Notice that the method implementation calls the super.addChild() method. The call to super.addChild() causes Flex to invoke the superclass's addChild() method to perform the operation. Your new functionality to open the Alert box occurs after the super.addChild() method.

You might have to use super() to call the base class method before your code, after your code, or not at all. The location is determined by your requirements. To add functionality to the method, you call super() before your code. To replace the base class method, you do not call super() at all.

The following example uses this component in an application:

<?xml version="1.0"?> 
<!-- as/MainHBoxWithAlert.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="myComponents.*">

    <mx:Script>
        <![CDATA[
            import mx.controls.Button;

            public function addButton():void {
                var myButton:Button = new Button();
                myButton.label = "New Button";
                myHBox.addChild(myButton);
            }
        ]]>
    </mx:Script>

    <MyComp:HBoxWithAlert id="myHBox">
    </MyComp:HBoxWithAlert>

    <mx:Button label="Add Button" click="addButton();"/>
    
</mx:Application>

The executing SWF file for the previous example is shown below: