You are here: Home > Programming > Flash programming > AS2 inheritance and the super call

AS2 inheritance and the super call

Print versionEdit this page
The following is of importance for a class hierarchy that is more than 2 levels deep, where the deepest level does not implement functions that are called in any of the superclass levels.

Depending on the chosen player to export for, multi-level inheritance in AS2 works slightly different. This is due to a bug in AS1 inheritance that had to be supported in AS2 for Flash player 6. In short, the bug is that the keyword "super" refers to the wrong superclass when called from a function that does not exist in the subclass it's called on. As it refers to the superclass of the subclass itself, instead of the superclass of the superclass, calling super actually calls the function in the superclass twice. Confusing? The example might clarify. Import the fla in MX2004, and run twice, once exported for player 6, once for player 7. The following traces are output:

- Flash player 6:

Constructor Class1 : this = object of type Class 1; inParam = 
Constructor Class1 : this = object of type Class 2; inParam = 
Constructor Class2 : this = object of type Class 2; inParam = 
Constructing new object of type Class3 with parameter 3
Constructor Class1 : this = object of type Class 3; inParam = 3
Constructor Class2 : this = object of type Class 3; inParam = 3
Constructor Class3 : this = object of type Class 3; inParam = 3

Calling myFunction on object of type Class3
myFunction Class3 : this = object of type Class 3
myFunction Class2 : this = object of type Class 3
myFunction Class1 : this = object of type Class 3
Calling notInTheSubclass()
notInTheSubclass Class2: this = object of type Class 3
notInTheSubclass Class2: this = object of type Class 3
notInTheSubclass Class1: this = object of type Class 3

Two notes here:

  1. the constructors of the two superclasses are called without parameter before actually creating the object. Due to the call to super() , the constructor of Class1 is called twice. Then the constructors are called again with parameter, in top-down order.
  2. The function notInTheSubclass exists in Class1 and Class2, not in Class3. See how it gets called twice for Class2. When running this in the debugger, you can see how the call to super.notInTheSubclass() in Class2 actually calls itself before calling the function in Class1.

- Flash player 7:

Constructing new object of type Class3 with parameter 3
Constructor Class1 : this = object of type Class 3; inParam = 3
Constructor Class2 : this = object of type Class 3; inParam = 3
Constructor Class3 : this = object of type Class 3; inParam = 3

Calling myFunction on object of type Class3
myFunction Class3 : this = object of type Class 3
myFunction Class2 : this = object of type Class 3
myFunction Class1 : this = object of type Class 3
Calling notInTheSubclass()
notInTheSubclass Class2: this = object of type Class 3
notInTheSubclass Class1: this = object of type Class 3
Here, the constructors are called only once, top-down, with parameter. Furthermore, the call to super.notInTheSubclass() in Class2 immediately refers to Class1, as expected in normal class hierarchy.

*Solution*
The workaround for this bug, when not exporting for player 7, is threefold:

  1. Make the call to super as early as possible. It might be necessary to save the state of the object if it gets changed by the call to super.
  2. Call the super with the same parameters, even if they are not all used in the function in the superclass. If you omit parameters, the second call to the class itself loses data.
  3. Keep the signature of the functions identical. The AS2 compiler does not check on this, so overloading functions is not possible.