304 Inheritance
If you don’t place a call to super() in the constructor function of a subclass, the compiler
automatically generates a call to the constructor of its immediate superclass with no
parameters as the first statement of the function. If the superclass doesn’t have a constructor,
the compiler creates an empty constructor function and then generates a call to it from the
subclass. However, if the superclass takes parameters in its definition, you must create a
constructor in the subclass and call the superclass with the required parameters.
Multiple inheritance, or inheriting from more than one class, is not allowed in ActionScript
2.0. However, classes can effectively inherit from multiple classes if you use individual
extends statements, as shown in the following example:
// not allowed
class C extends A, B {} // **Error: A class may not extend more than one
class.
// allowed
class B extends A {}
class C extends B {}
You can also use interfaces to implement a limited form of multiple inheritance. For more
information on interfaces, see Chapter 9, “Interfaces,” on page 313. For an example that
creates a subclass, see “Example: Extending the Widget class” on page 304. For additional
information on
super, see super statement in the ActionScript 2.0 Language Reference.
Example: Extending the Widget class
Class members propagate to subclasses of the superclass that defines those members. The next
example demonstrates how you could create a Widget class, which you extend (subclass) by
writing a class named SubWidget.
To create the Widget class and SubWidget subclass:
1. Create a new ActionScript file and save it as Widget.as.
2. Add the following code to the new document:
class Widget {
public static var widgetCount:Number = 0;
public function Widget() {
Widget.widgetCount++;
}
}
3.
Save your changes to the ActionScript file.
4. Create a new ActionScript file and save it as SubWidget.as in the same directory as the
Widget class.