extends statement

Usage 1:

class className extends otherClassName {}

Usage 2:

interface interfaceName extends otherInterfaceName {} 

Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash tab of your FLA file's Publish Settings dialog box. This keyword is supported only when used in external script files, not in scripts written in the Actions panel.

Defines a class that is a subclass of another class; the latter is the superclass. The subclass inherits all the methods, properties, functions, and so on that are defined in the superclass.

Interfaces can also be extended using the extends keyword. An interface that extends another interface includes all the original interface's method declarations.

Availability: ActionScript 2.0; Flash Lite 2.0

Parameters

className:String - The name of the class you are defining.

Example

In the following example, the Car class extends the Vehicle class so that all its methods, properties, and functions are inherited. If your script instantiates a Car object, methods from both the Car class and the Vehicle class can be used.

The following example shows the contents of a file called Vehicle.as, which defines the Vehicle class:

class Vehicle { 
 var numDoors:Number; 
 var color:String; 
 function Vehicle(param_numDoors:Number, param_color:String) { 
 this.numDoors = param_numDoors; 
 this.color = param_color; 
 } 
 function start():Void { 
 trace("[Vehicle] start"); 
 } 
 function stop():Void { 
 trace("[Vehicle] stop"); 
 } 
 function reverse():Void { 
 trace("[Vehicle] reverse"); 
 } 
}

The following example shows a second AS file, called Car.as, in the same directory. This class extends the Vehicle class, modifying it in three ways. First, the Car class adds a variable fullSizeSpare to track whether the car object has a full-size spare tire. Second, it adds a new method specific to cars, activateCarAlarm(), that activates the car's anti-theft alarm. Third, it overrides the stop() function to add the fact that the Car class uses an anti-lock braking system to stop.

class Car extends Vehicle { 
 var fullSizeSpare:Boolean; 
 function Car(param_numDoors:Number, param_color:String, param_fullSizeSpare:Boolean) { 
 this.numDoors = param_numDoors; 
 this.color = param_color; 
 this.fullSizeSpare = param_fullSizeSpare; 
 } 
 function activateCarAlarm():Void { 
 trace("[Car] activateCarAlarm"); 
 } 
 function stop():Void { 
 trace("[Car] stop with anti-lock brakes"); 
 } 
}

The following example instantiates a Car object, calls a method defined in the Vehicle class (start()), then calls the method overridden by the Car class (stop()), and finally calls a method from the Car class (activateCarAlarm()):

var myNewCar:Car = new Car(2, "Red", true); 
myNewCar.start(); // output: [Vehicle] start 
myNewCar.stop(); // output: [Car] stop with anti-lock brakes 
myNewCar.activateCarAlarm(); // output: [Car] activateCarAlarm

A subclass of the Vehicle class can also be written using the keyword super, which the subclass can use to access properties and methods of the superclass. The following example shows a third AS file, called Truck.as, again in the same directory. The Truck class uses the super keyword in the constructor and again in the overridden reverse() function.

class Truck extends Vehicle {
 var numWheels:Number;
 function Truck(param_numDoors:Number, param_color:String, param_numWheels:Number) { 
 super(param_numDoors, param_color); 
 this.numWheels = param_numWheels; 
 } 
 function reverse():Void { 
 beep(); 
 super.reverse(); 
 } 
 function beep():Void { 
 trace("[Truck] make beeping sound"); 
 } 
}

The following example instantiates a Truck object, calls a method overridden by the Truck class (reverse()), then calls a method defined in the Vehicle class (stop()):

var myTruck:Truck = new Truck(2, "White", 18); 
myTruck.reverse(); // output: [Truck] make beeping sound [Vehicle] reverse 
myTruck.stop(); // output: [Vehicle] stop

See also

class statement