O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.9. Implementing Subclass Versions of Superclass Methods

Problem

You want to write a subclass implementation of a superclass method (that is, you want a subclass to have a different implementation of a particular method than its superclass).

Solution

Add a method to the subclass with the same name as the superclass method. If you want to call the superclass method from within the subclass method, use the super keyword to reference the superclass.

Discussion

Although a subclass inherits all the methods and properties of a superclass, a subclass does not need to use the superclass implementations. Sometimes, a superclass method may be a generic implementation of particular functionality, and you want to create a more specific implementation within the subclass. When you explicitly define a method within the subclass, the ActionScript interpreter does not search up the prototype chain to find any other versions of that method (the prototype chain is the hierarchy of prototype properties from the subclass up to any superclasses that it inherits from). However, if you want a subclass implementation to call the superclass version of the method, use the super keyword to reference the superclass from within the subclass. For example:

// Define a superclass.
_global.MySuperClass = function (  ) {};

// Create a superclass implementation of myMethod(  ). MySuperClass.prototype.myMethod = function ( ) { return 12; }; // Create a subclass. _global.MySubClass = function ( ) {}; // Define the inheritance ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required