ActionScript 3 inheritance: developers beware!

! Warning: this post hasn't been updated in over three years and so may contain out of date information.

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):

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:

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

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:

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:

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.

6 thoughts on “ActionScript 3 inheritance: developers beware!

  1. 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…

  2. 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…

  3. @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.

  4. 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.

Comments are closed.