ActionScript 3 inheritance: developers beware!
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.
TweetShare This Post...
6 comments so far, click here to read them or add another
6 Comments so far


Ugly ugly ugly, it’s a mickey mouse compiler
Nice workaround.
It’s interesting to see how Java and C developers think differnet ways.
But IMHO this is good restriction in AS3.
If you have a habit of unit-testing your code, you know that it is bad habit to use statics. Static inheritance smells like bad thinking, after all for what and where do we need so many static classes?
Actually, there is not even an abstract class in AS3, but you can still use any class as abstract if you need one.
Even if some old language is capable of doing something, it doesn’t mean that we should be always able to do that. Comparing languages you should always think wider, than just one detail.
-keep up the good work…
You make a very good point dejavu: statics should be used with care as they do cause all sorts of problems with unit testing and can cause strong coupling in the code. I came across this with static inheritance when investigation how to unit test a PureMVC system, as it uses two strong code smells: statics and singletons. But more on that in a month or so when I finish the work…
you can always do the old BaseClass.method()
@john,
I agree: BaseClass.method() is the best way with AS3. Using BaseClass.method() requires a paradigm shift by those used to using Java, C# etc though.
Statics are very useful, sometimes it can be the most elegant solution to a problem (even if it makes your code a little less modular). It’s hard to take AS3 seriously when it can’t do what is normal in any other mainstream languages. I say this as a guy who makes a living working with flash.
It’s very similar to how you can’t include parameters in an AS3 interface. Doesn’t make any sense. Adobe should look at HaXe, it’s more of a real language and compiles your swf file to run up to 3 times faster.