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:
class Parent {
}
class Child extends Parent {
}
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 replies on “Inheritance in ActionScript 2.0”
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?
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?
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?)
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
I was wondering if you may have some feedback for me on a post I have on the Flash Forums @ Macromedia:
http://webforums.macromedia.com/flash/messageview.cfm?catid=288&threadid=689492
Thanks for any help in advance
Kerphi,
You’re right – no multiple inheritance in AS2; interface is the way to go. Besides, favor composition over inheritance whenever possible.
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.
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
JLM,
The superclass invocation bug (during inheritance setup) is fixed in Flash Player 7, but not for FP6.
For your other question, perhaps this article I wrote may help: http://www.quantumwave.com/flash/dynamicMethod.html
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
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
Justin,
Static methods are always referenced from the class, just like Math.random().
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
Can i use final methods in abstract class?