What is this? From this page you can use the Social Web links to save ActionScript 3 inheritance: developers beware! to a social bookmarking site, or the E-mail form to send a link via e-mail.

Social Web

E-mail

E-mail It
September 25, 2009

ActionScript 3 inheritance: developers beware!

Posted in: ActionScript

Following on from my recent post regarding variable scope within ActionScript, I’m going to explain another “gotcha” that can catch out developers used to “proper” OO languages, such as C# and Java. That gotcha relates to class/ static member inheritance.

Consider the following C#/ Java code (ignoring package-level considerations, it will compile in either language):

  public class BaseClass
  {
    public static int i;
    public static void method() { }
  }

  class ChildClass: BaseClass
  {
  }

  class TestClass
  {
    public TestClass()
    {
      int j = ChildClass.i;
      ChildClass.method();
    }
  }

With C# and Java, there is nothing spectacular about this code. Sadly though, achieving this functionality in ActionScript requires a significant work-around. If we simply rewrite the above using AS3 syntax, we get the following errors:

  public class BaseClass
  {
    public static var i:int;
    public static function method():void { }
  }

  public class ChildClass extends BaseClass
  {
  }

  public function TestClass()
  {
    var j:int = ChildClass.i;
    ChildClass.method();
  }

and this code won’t compile. Instead we get:

1061: Call to a possibly undefined method method through a
      reference with static type Class.	TestClass.as
      line 4 Flex Problem
1119: Access of possibly undefined property i through a
      reference with static type Class.	TestClass.as
      line 3 Flex Problem

As the “Static properties not inherited” section of the language reference explains, AS3 doesn’t support static member inheritance, thus the errors. This limitation can be worked around with a rather ugly bodge. Modify ChildClass to the following:

  public class ChildClass extends BaseClass
  {
    public static function get i():int { return BaseClass.i; }
    public static function set i(val:int):void { BaseClass.i = i; }

    public static function method():void { BaseClass.method(); }
  }

and the errors go away. Having to do this for every class that inherits from a parent with static members though would be both tedious and unmaintainable. It is therefore probably more practical to simply accept this shortfall with the AS3 language and work with it, rather than fighting it.

As a final point, the scoping rules of AS3 cause a weird effect related to this issue. Consider the following version of ChildClass:

  public class ChildClass extends BaseClass
  {
    private var j:int = i;

    public function ChildClass()
    {
      method();
    }
  }

This code compiles just fine. Even though i and method aren’t accessible via ChildClass for code external to ChildClass, within that class, all parent and other ancestor static members are accessible.


Return to: ActionScript 3 inheritance: developers beware!