OOP4AS2#5: Inheritance (extends and super)

Entry #5 in this series will deal with implementing class inheritance with the “extends” and “super” keywords. In this post, I will focus on the syntactic details of inheritance in ActionScript 2.0, and will likely post my opinions and thoughts how to apply that syntax separately.

Inheritance is a very important concept in Object Oriented Programming, as it allows for the development of highly modular and extensible code, which in turn reduces development time. It also allows developers to extend the functionality of pre-existing classes, without touching the original code.

Once again, this post builds on previous posts, so at the very least you should grab the code from entry 4 before proceeding. Also, if you’re looking for a great introduction to all the new goodies in F04 (Flash MX 2004), be sure to pick up the forthcoming “FlashMX 2004 Demystified” from Macromedia Press.

Read on…

While nothing has really changed under the hood in how the Flash Player deals with inheritance, the syntax has been cleaned up significantly. We now use the “extends” keyword to indicate that a new class inherits from an existing class like so:

NOTE: For these examples, we’ll do a bunch of coding in the class file, then test it all in the FLA at the end, all at once.

// in UltraGThing.as:
class UltraGThing extends GThing {
}

This will create a new class called UltraGThing that inherits all of the members (properties and methods) of the GThing class we defined in the previous entries. These members are then available to all instances of the UltraGThing class. The class you inherit from (GThing) is called the super class, while the class that is doing the inheriting (UltraGThing) is called the sub class.

You can then extend the inherited functionality by adding new members to the UltraGThing class. For example, below I have added an ultraPower property, and a getUltraPower method:

// in UltraGThing.as:
class UltraGThing extends GThing {
var ultraPower:String = "none";
function getUltraPower():String {
return ultraPower;
}
}

You can also overwrite methods of the super class in your sub class. If you do so though, you will often want to be able to reference the original methods of the super class – we can do this by using the super keyword. For example, if I wanted to modify the getGreeting method of GThing to insert “Ultra” after the salutation, I could do so as such:

// in UltraGThing.as:
class UltraGThing extends GThing {
var ultraPower:String = "none";
function getUltraPower():String {
return ultraPower;
}
function getGreeting(p_salutation:String):String {
// call the GThing getGreeting method with
// "Ultra" inserted and return the result
return super.getGreeting(p_salutation+" Ultra");
}
}

On a related note, super() can be used to call the constructor of the super class from within the sub class. Note that super() must be the first instruction within the sub class’s constructor. Let’s use this capability to add a constructor for UltraGThing that sets the new ultraPower property as well as setting the myName property by calling the super class’s constructor:

// in UltraGThing.as:
class UltraGThing extends GThing {
var ultraPower:String = "none";
// define the constructor:
function UltraGThing(p_myName:String,p_ultraPower:String){
// set myName by calling GThing constructor:
super(p_myName);
ultraPower = p_ultraPower;
}
function getUltraPower():String {
return ultraPower;
}
function getGreeting(p_salutation:String):String {
return super.getGreeting(p_salutation+" Ultra");
}
}

Now, let’s test it all out in the FLA:

var myG:UltraGThing = new UltraGThing("Stinker","smelling bad");
trace(myG.getGreeting("Hello"));
trace("Power is: "+myG.getUltraPower());

It should trace out:
“Hello Ultra Stinker”
“Power is: smelling bad”

Wheww, that wraps this topic up. There is more to come…

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

3 Comments

  1. When I grow up, I wanna be just like you! 😛

  2. >>While nothing has really changed under the hood in how the Flash Player deals with inheritance…<<

    Flash Player 7 has a new “extends” bytecode.

  3. This article is now available in german!

    Check it out at:

    http://www.urbanspice.de/blog/archives/000017.html

Comments are closed.