AS2 OOP: Inheritance
         by senocular

Extends
The keyword that makes inheritance possible with ActionScript 2.0 is extends. This keyword allows for a class to inherit properties and methods from another. It's used once in the class definition directly after the name of the class being defined. Following the extends keyword is the class name of the class you intend to inherit from - the intended super class.

class thisClassName extends superClassName { ...

The following example is a new class that extends or inherits from the class Person.

class Geek extends Person {
static var OSofChoice:String = "Linux";
var iq:Number;
 
function Geek (n:Number) {
iq = n;
}
 
function doYouKnow (language:String):Boolean {
switch (language) {
case "C++": return true;
case "Java": return true;
case "PHP": return true;
case "Laymen's": return false;
default: return true;
}
}
}

Geeks are people too, right? With Geek extending Person, it now has gained all properties and has access to all methods within the Person class. Here is the ActionScript 1.0 equivalent.

/* ActionScript 1.0 */
Geek = function(n) {
iq = n;
};
Geek.OSofChoice = "Linux";
Geek.prototype = new Person(); // inherit from Person
Geek.prototype.doYouKnow = function(language) {
switch (language) {
case "C++": return true;
case "Java": return true;
case "PHP": return true;
case "Laymen's": return false;
default: return true;
}
};

Notice the term "prototype" in the ActionScript 2.0 version is nowhere to be found. Its simply a matter of class extends super class and you're done.

Now, when I said using extends allowed access to all the methods of the super class, I meant all the methods; normal class methods, private methods and even static methods. That's right, static methods too, something you didn't so easily get with ActionScript 1.0. In ActionScript 2.0, they're readily accessible and easy to access. Lets start another example that better demonstrates this. Two classes with simple descriptive names to help keep track of what's going on (nothing gimmicky or too amusing like the Geek class I'm sure so many of you could relate so well to ;)

// in SuperClass.as
class SuperClass {
static var ssp:String = "super static property";
public var spubp:String = "super public property";
private var sprip:String = "super private property";
 
static function ssm():Void {
trace("super static method");
}
public function spubm():Void {
trace("super public method");
}
private function sprim():Void {
trace("super private method");
}
}
// in SubClass.as
class SubClass extends SuperClass {
function SubClass() {
trace(ssp);
trace(spubp);
trace(sprip);
ssm();
spubm();
sprim();
}
}
// in Flash movie
var instance:SubClass = new SubClass();
 
/* output:
super static property
super public property
super private property
super static method
super public method
super private method
*/

Anything usable in the super class is also usable in the sub class thanks to extends. It takes care of everything for you. Just be careful to note that this is the class itself and not necessarily the instances of that class. Instances, for example, can't directly access an inherited static property. Only other methods within the subclass (which themselves can be called from an instance) can do that. Instances are restricted to non-static methods.

With that in mind, let's take another look at Math2. Remember Math2 was an object that provided a space to add Math related functions, such as randRange, since now, by default, the Math object no longer allows custom functions to be added to it (it's not a dynamic class). Because extends allows inheritance of all class methods, even static ones, we might consider making Math2 extend Math, right?

class Math2 extends Math {
// ... custom Math functions ...
}

But what does this do? Doesn't it give access of Math related static methods to Math2? Well it does, at least to Math2 methods. It does not, however, give the methods to the Math2 class object itself. It's that Math2 object which is used in calling those methods in the first place. Well, what about an instance of Math2 then? Nope. Same issue there. An instance of Math2 is no more capable of calling inherited static methods as Math2 itself is. You'll just have to use your own custom math methods in Math2 and use Math when you need native Flash math methods. This doesn't mean you can't still extend the Math class, though. At least Math methods won't have to be referenced using the Math object.

class Math2 extends Math {
static function randRange(low:Number, high:Number):Number {
// readily accessible Math methods like floor
// random is an exception because of the
// existence of the top level function random()
return low + floor(Math.random()*(high-low+1));
}
}

The extends keyword can also be used with interfaces. Interfaces using extends can only extend other interfaces and not other classes. This lets you include definitions from one interface into another very easily.

// in SimpleBlockingStyle.as
interface SimpleBlockingStyle {
function block():Void;
}
// in SimpleFightingStyle.as
interface SimpleFightingStyle extends SimpleBlockingStyle {
function headButt():Boolean;
function kick(foot:String):Boolean;
}

You can also use extends with classes implementing interfaces, but you'll need to be sure to use the extends keyword before implements. This is a requirement of the compiler.

class className extends superClass implements interface { ...

When you inherit from a super class this way, those super class methods, now accessible to the current (sub) class, will be accounted for in the interface interface. So if you think about it, the compiler, when reading this line of code, will need to know all the methods that class has access to (its own and those inherited) so it can properly check to see if those outlined in the interface are present. This might help you remember the order.

class FookYoo extends MartialArt implements SimpleFightingStyle {
// ...
}

Despite the apparent ease of access to the super class, sometimes you may still need to be more specific in you're referencing of that class. That can be accomplished using super.

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.