Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Inheritance > About writing subclasses in Flash > About writing a subclass | |||
The following code defines the custom class JukeBox, which extends the Sound class. It defines an array called song_arr and a method called playSong(), which plays a song and invokes the loadSound() method that it inherits from the Sound class.
class JukeBox extends Sound {
public var song_arr:Array = new Array("beethoven.mp3", "bach.mp3", "mozart.mp3");
public function playSong(songID:Number):Void {
super.loadSound(song_arr[songID], true);
}
}
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 Interfaces. For an example that creates a subclass, see Example: Extending the Widget class. For additional information on super, see super statement in the ActionScript 2.0 Language Reference.
Flash CS3