Inheritance in ActionScript 2.0

Back in the good old days of Flash 5 and MX, trying to do inheritance in ActionScript required some knowledge of what goes on behind the scene. Also, the infamous superclass invocation bug haunted developers for almost three years.

Now in ActionScript 2.0, with the new OOP-specific keywords, inheritance is much simpler:

That’s all. And the keyword “super()” in the Child class constructor is not even mandatory because the compiler inserts it for you!

As for the superclass invocation bug, it is now fix for Flash player 7!

14 thoughts on “Inheritance in ActionScript 2.0

  1. Hi Dave,
    Does the compiler also provide the default (no-args) constructor if we omit it in the class definition (as expected)?
    Also, -afaik- there is no method overloading in AS 2.0 (at least in this release), so there will be no constructor overloading. How do we simulate constructor overloading? By providing various static methods that return instances of our class?

  2. Hi Ahmet,

    That’s right, there is no overloading of the constructor in AS2 because it all compiles down to AS1. To simulate constructor overloading, you’d do the same thing as in AS1. The simplest solution is to check arguments.length and handle differently.

    I’m not sure if I follow “By providing various static methods that return instances of our class”. Could you elaborate?

  3. I was thinking if a class could provide a static method that returns an instance of class based on different types of input (Factory ?).
    For example,

    static function getInstance(input)
    {
    switch(input)
    {
    case A:
    // instantiate the class
    // according to input
    return classInstance;
    break;
    case B:
    // etc.
    }
    }

    So we can check the arguments.length, but they have to be of same type (all Number, all String?)

  4. Hi,

    Is it also possible to do multi-inheritance ?
    I think it’s not possible, so the solution is to use interfaces I think ? (like in java langage)

    Kerphi

  5. Derek,

    Peter’s response is pretty much what I’d have recommended as well. Keeping a reference to the scope of the calling object is pretty much the way to get to it within the XML object.

  6. Dave

    how do you do this in AS1, I rem there were problems with inheritance in AS1 are they fixed in player7. I want to do it in AS1 so that I can dynamically create a class and have it inherit from another one and not be tied to a specific file name/class name. Any ideas greatly recieved.

    JLM

  7. Dave

    Thanks that is very helpfull. I have lots of questions but I will restrain myself to one, you used eval() could you use this[] or something instead?

    JLM

  8. Dave

    The code is very interesting I have made some progress but it appears that the ‘static’ methodClass only works on the class not on any instances of the class? should it be in the constructor or is there something missing?

    JLM

  9. Hi!,
    this is a bit off-topic but related to OOP.

    I have been working for last 3+ years with Flash, C, C++, VB, PHP etc.
    Lately i have started feeling that i am not good in OOP. I wonder whats the best way to learn it, how to get started, what examples to do?

    please share your views.

    Regards,
    Abdul Qabiz

Leave a Reply

Your email address will not be published. Required fields are marked *

forty ÷ = 8