AS3 static inheritance: developers beware! (revisited)

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

A while ago I wrote an article on the oddities of AS3 with regards to its lack of static inheritance. As part of my AS4 proposals series, I started writing a proposal for introducing static inheritance to AS4. In the process of writing it, I delved deeper into this topic and found that AS3 static inheritance is a truly strange beast indeed. It can be summed up neatly (though probably confusingly) as “for a class B that inherits from a class A, public static methods of A are inherited as protected static methods (which cannot be overridden) by class B”

Update: please see the comments associated with this article. I made a fundamental mistake with it, which renders parts of it nonsense. At some point, I’ll find time to rewrite it!

To illustrate what I mean by the above, consider the following three classes:

So class A defines three instance methods test1(), test2() and test3() that set aVar, bVar and cVar to 1 respectively.

Class B defines an overridden version of test1(), which still calls staticFunc1(). It also defines a new version of staticFunc1(), though the overridden keyword is not used. In fact the overridden keyword cannot be used as the compiler will complain if it is.

Class C defines an overridden version of test2(), which sets A’s static var bVar directly. It also defines a new version of staticFunc3().

Let’s consider what we might expect to happen with “normal” OO static inheritance when c is an instance of class C and c.test1(), c.test2() and c.test3() are called:

c.test1() – There is no test1 in C, but there is in B. This method is run. It calls staticFunc1(). There is no staticFunc1 in C, but again there is in B. aVar is set to 2 therefore.

c.test2() – There is a test2 in C. It is run and bVar is set to 3.

c.test3() – There is no test3 in C or B, but there is in A. This method is run. It calls staticFunc3(). There is a staticFunc3 in C. cVar is set to 3 therefore.

If we examine what happens with AS3 though, the results are slightly different, so the following code

gives the following result:

As you can hopefully see, the results are not the same. We get aVar=2, bVar=3, cVar=1, rather than the expected aVar=2, bVar=3, cVar=3. The static inheritance almost works as expected. The exception is that c.test3 behaves like:

c.test3() – There is no test1 in C or B, but there is in A. This method is run. It calls staticFunc3(). There is a staticFunc3 in class A. cVar is set to 1 therefore.

So in conclusion, AS3 has static inheritance of sorts, but care must be taken when using it as it behaves strangley at times. One cannot do c.aVar for example, but within class C one can reference aVar. Thus my original comment that public static methods of a class are inherited as protected static methods by its subclasses. And standard inheritance rules do not apply, so accessing static methods from base class instance methods can lead to unexpected results.

3 thoughts on “AS3 static inheritance: developers beware! (revisited)

  1. Have you actually tried verifying your claim about “normal” static OO inheritance? As in, writing the code in what you might consider a “normal” OO language? For example in Java? Had you done that, you would’ve seen that the expected result is “aVar = 2, bVar = 3, cVar = 1”, just like in AS3.

    Now let me enlighten you about why that is so: there is no such thing as “static polymorphism”. The keyword “static” has been sadly overloaded with two meanings:

    1) It declares a class member instead of an instance member.

    2) It declares an early-bound member instead of a late-bound member.

    Guess which one of those two corresponds to the word “static”? That’s right, the second meaning. Static members are not subject to polymorphism because they are bound early, not late. You cannot override a static member, period.

    Look, it’s nothing personal, but I’ve read several of your blog posts where you’ve been patently incorrect about OO. So I have a suggestion: do some research before posting. At the very least, write and run some code to verify your claims.

  2. @Vojislav,

    I tested it in C# … and you are absolutely right: it gives the same result as the AS3 code. Looks like a rewrite of this post is required as “normal” OO does not work as I thought with regard to statics.

    Thanks for taking the time to point out my mistake.

Comments are closed.