Pages

Tuesday, October 19, 2010

AS3: Override in Actionscript 3

To understand the working of override keyword in Actionscript 3, follow the article.

Create a blank .fla file and name it Main.fla. Then fill in Document class input box, by typing Main in it. This will connect the Main.as file with the Main.fla file.

Main.as

package
{
  import flash.display.MovieClip;
  /**
   * ...
   * @author Abhishek Kumar
   */
  public class main extends MovieClip
  {
    public function main()
    {
      var oBase:CBase = new CBase();
      oBase.method();

      var oChild:CChild = new CChild();
      oChild.method();

      var oClone:CBase = oChild as CBase;
      oClone.method();
    }
  }
}


CBase.as

package
{
  public class CBase
  {
    public function CBase()
    {
    }
    public function method():void
    {
      trace("CBase -> method");
    }
  }
}


CChild.as

package
{
  public class CChild extends CBase
  {
    public function CChild()
    {
    }
    public override function method():void
    {
      trace("CChild -> method");
    }
  }
}


OUTPUT

CBase -> method
CChild -> method
CChild -> method


Hence, you have seen how the override keyword works in Actionscript 3.

No comments:

Post a Comment