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 ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.