AS1 OOP: Inheritance
by senocular
Scripting Inheritance
Applying inheritance in Actionscript with classes usually
consists of two parts, setting up the inheritance chain
connection and super class initialization. Setting up the
inheritance connection to the super class is key. It pretty
much defines the act of inheritance. Initialization of the
super class, however, can be dependant on your class
definitions and may not be needed at all.
With Actionscript, the standard method of establishing
inheritance is through the prototype object of your class.
After all, the prototype is the object that shares. Linking
inheritance through the prototype means subclass instances
will be able to share super class methods etc. This is setup
is by immediately defining the prototype of a class to be a
new instance of the super class – before any other
prototypes of the subclass are defined. Taking the pelican
example to a simplification, we can have two classes, Animal
and Bird. The Bird class can then be a subclass of Animal.
- // Animal - a super class to Bird
- Animal = function(){
- this.isAnAnimal = true;
- };
- // Bird - a subclass of Animal
- Bird = function(){
- this.isABird = true;
- };
- // setup inheritance linkage between Bird and Animal
- Bird.prototype = new Animal();
- // we can see the bird being an animal through an
instance of bird
- poly = new Bird();
- trace(poly.isABird); // traces true
- trace(poly.isAnAnimal); // traces true
The reason this is performed before other prototype
definitions is because doing this actually replaces the
prototype object with a new object – an instance of the
super class. Prototypes can then be added specifically for
the subclass. If they were added before, all those
definitions would be lost as the prototype is replaced with
a new instance of the super class. As an instance of the
super class, though, the prototype object inherently then
has access to all super class properties and methods as
every other instance of the super class has. And because its
now serving as the subclass prototype, it means all
instances of the subclass, sharing its properties and
methods, will then have access properties and methods of
that super class. Basically all you're doing is quickly
creating a pre-functional prototype object for your class -
a prototype object with already defined methods and
properties as they exist in another class. It's a quick and
easy way to incorporate pre-written code into your new class
without having to write them all over again.
Though this new prototype instance is in fact an direct
instance of the class acting as the super class, it is not
an acting instance of that class -not an instance which is
being used as a normal instance of that class would be. That
being the case, if you are using a static property to keep
track of class instances, you may need to compensate for
instances used to define inheritance in this manner. You can
do this either with some form of if check within the
constructor or just by adjusting the count or corresponding
value after the prototype assignment.
Let's do another example, this time with more going on in
prototypes. Here we'll use Person and SuperHero classes.
- // Person class definition
- Person = function(name, age){
- this.name = name;
- this.age = age;
- };
- Person.prototype.speak = function(phrase){
- trace(phrase);
- };
- // SuperHero class definition
- SuperHero = function(power){
- this.superPower = power;
- this.peopleSaved = new Array();
- };
- SuperHero.prototype = new Person();
- SuperHero.prototype.savePerson = function(person){
- this.peopleSaved.push(person);
- // a SuperHero can use speak because its inherited
- // from its super class Person
- this.speak("A SuperHero's job is never done!");
- };
- SuperHero.prototype.getLastPersonSaved = function(){
- return this.peopleSaved[this.peopleSaved.length-1];
- };
- // create some instances.
- damselInDistress = new Person("Lois", 25);
- superMan = new SuperHero("Being subclass instance.");
- // use the new instances and their methods
- damselInDistress.speak("Help!"); // traces "Help!"
- superMan.savePerson(damselInDistress); // traces "A
SuperHero’s job is never done!"
- // check to see if Lois was actually saved
- trace(superMan.getLastPersonSaved() ==
damselInDistress); // traces true
Ironically, SuperHero here is a subclass, but don’t let
that confuse you. Person is actually the super class to
SuperHero. But as you can see, as a Person, our superMan
instance can speak just like other people – like the
damselInDistress. Being the SuperHero that he is, though,
superMan also has a super power, an array of saved people
and has new methods, savePerson and getLastPerson saved.