Static methods 101 in AS3 revisit


1. Inheritance (of a member method) means the derived class keeps a copy of the base class member method. Static methods are not inherited and cannot be overridden. To override a method means to redefine the behavior of an inherited method.

2. Keywords “private”, “protected”, “internal” are used to control scope/accessibility of the members as to other classes (i.e., if the code can be used by others).

In AS3, private members are only visible to the class itself, and NOT to the subclasses. If you want to use (call a method or refer to a property) members from a superclass, you need to mark them as “protected”.

In AS3, protected members are visible to the class and all subclasses. Internal members are visible to all classes within the same package.

3. Override. When you “override” a class method, you “specify that a method replaces an inherited method”. This means the “overridden” method will be invoked in place of the inherited method, even if you just define the signature of the method (i.e., a null method, like so

override protected function myfoo():void{}

Overriding variables is not permitted. To override a property means to redefine the property in a subclass.

4. It’s permissible to define an instance property using the same name as a static property within a class.

5 Responses to “Static methods 101 in AS3 revisit”

  1. Frank Wienberg Says:

    Sorry to contradict, but your bullet 1. is a common misconception. Static members are *not* inherited, however subclasses can access them through the scope chain. The difference is that while you can access static members in a subclass without any qualification, from “outside”, you can only access them exactly through the class in which they are defined, not through a subclass.
    Quotes from the livedoc page you cite:

    “Static properties not inherited

    Static properties are not inherited by subclasses. This means that static properties cannot be accessed through an instance of a subclass. A static property can be accessed only through the class object on which it is defined.”


    “Static properties and the scope chain

    Although static properties are not inherited, they are within the scope chain of the class that defines them and any subclass of that class. As such, static properties are said to be in scope of both the class in which they are defined and any subclasses. This means that a static property is directly accessible within the body of the class that defines the static property and any subclass of that class. “

  2. Og2t Says:

    OK, thanks for the explanation, but how can I access static variables from the subclass – even if they cannot be accessed, there must be a way to do it, maybe through another method?

    Would something like that work?

    public function getStaticVariable(name:String):*
    return this["name"];

  3. Og2t Says:

    I’ve figured it out, you can use a static getter in the subclass, that returns the static var/const from the superclass (BaseClass).

    public static get MY_VAR():* { return BaseClass.MY_VAR; }

  4. Frank Wienberg Says:

    Yes, that is possible. The question is why don’t you access the static variable through the BaseClass in the first place? Maybe it does not belong to the public API?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: