6.1 A Primer on Inheritance

 <  Day Day Up  >  

Let's consider a very simple, abstract example to get a feel for how inheritance works (we'll get into practical applications once we cover the basic syntax). Here's a class named A , with a single method, x( ) , and a single property, p :

 class A {   public var p:Number = 10;   public function x ( ):Void {     trace("Method x( ) was called.");   } } 

As usual, we can create an instance of class A, invoke method x( ), and access property p like this:

 var aInstance:A = new A( ); aInstance.x( );       // Displays: Method x( ) was called. trace(aInstance.p);  // Displays: 10 

Nothing new so far. Now let's add a second class, B , that inherits method x( ) and property p from class A . To set up the inheritance relationship between A and B , we use the extends keyword to indicate that class B inherits class A 's method and property definitions:

 class B   extends   A {   // No methods or properties defined. } 

figs/as1note.gif ActionScript 1.0 offered two ways to establish inheritance. Here's the official technique:

 function Class A ( ) { ... } function Class B ( ) { ... } ClassB.prototype = new Class A( ); 

Here's the undocumented technique:

 function Class A ( ) { ... } function Class B ( ) { ... } ClassB.prototype._ _proto_ _ = ClassA.prototype; 

To learn the difference between these approaches, consult Chapter 12 of ActionScript for Flash MX: The Definitive Guide (O'Reilly), which covers the topic in depth.

Don't confuse extending a class with augmenting or enhancing a class's features. When class B extends class A, no changes whatsoever are made to class A. Contrast this with a case in which we add a new method to a dynamic class such as MovieClip . The latter is simply enhancing an existing class and has nothing to do with inheritance.

Now here's the neat part about extending a class. Because class B extends (inherits from) class A , instances of B can automatically use the method x( ) and the property p (even though class B does not define that method or property directly):

 var bInstance:B = new B( ); bInstance.x( );       // Displays: Method x( ) was called. trace(bInstance.p);  // Displays: 10 

When bInstance.x( ) is invoked, the interpreter checks class B for a method named x( ) . The interpreter does not find method x( ) defined in class B ; so it checks B 's superclass (i.e., the class that B extends), class A , for the method. There, the interpreter finds x( ) and invokes it on bInstance .

Notice that class B does not define any methods or properties of its own. In practice, there isn't much point in defining a class that doesn't add anything to the class it extends; therefore, doing so is usually discouraged. Normally, class B would define its own methods and/or properties in addition to inheriting A 's methods and properties. That is, a subclass is really a superset of the features available in its superclass (the subclass has everything available in the superclass and more). Accordingly, here is a more realistic version of class B , which inherits method x( ) and property p from class A , and also defines its own method, y( ) :

 class B extends A {   public function y ( ):Void {     trace("Method y( ) was called.");   } } 

Now instances of B can use all the methods and properties of both B and its superclass, A :

 var bInstance:B = new B( ); // Invoke inherited method, defined by class   A   . bInstance.x( );       // Displays: Method x( ) was called. // Invoke method defined by class   B   . bInstance.y( );       // Displays: Method y( ) was called. // Access inherited property. trace(bInstance.p);  // Displays: 10 

Thus, class B is said to specialize class A . It uses the features of class A as a base on which to build, adding its own features or even ”as we'll see later ”overriding A 's features with versions modified for its own needs. Accordingly, in an inheritance relationship between two classes, the extended class (in our case, class A ) is called the base class , and the class that does the extending (in our case, class B ) is called the derived class . However, the terms "base class" and "derived class" have several synonyms, including superclass and subclass , parent and child , and type and subtype .

Inheritance can (and often does) involve many more than two classes. For example, even though class B inherits from class A , class B can act as a base class for another class. The following code shows a third class, C , that extends class B and also defines a new method, z( ) . Class C can use all the methods and properties defined by itself, its superclass ( B ), or its superclass's superclass ( A ):

 class C extends B {   public function z ( ):Void {     trace("Method z( ) was called.");   } } // Usage: var cInstance:C = new C( ); // Invoke method inherited from   A   . cInstance.x( );  // Displays: Method x( ) was called. // Invoke method inherited from   B   . cInstance.y( );  // Displays: Method y( ) was called. // Invoke method defined by   C   . cInstance.z( );  // Displays: Method z( ) was called. // Access property inherited from   A   . trace(cInstance.p);  // Displays: 10 

Furthermore, a single superclass can have any number of subclasses (however, a superclass has no way of knowing which subclasses extend it). The following code adds a fourth class, D , to our example. Like class B , class D inherits directly from class A . Class D can use the methods and properties defined by itself and by its superclass, A .

 class D extends A {   public function w ( ):Void {     trace("Method w( ) was called.");   } } 

With four classes now in our example, we've built up what's known as an inheritance tree or class hierarchy . Figure 6-1 shows that hierarchy visually. Note that a single subclass can't have more than one direct superclass, but its superclass can have a superclass, allowing the hierarchy to continue ad nauseum.

Figure 6-1. Example class hierarchy
figs/as2e_0601.gif

All OOP applications can be depicted with a class diagram such as the one shown in Figure 6-1. In fact, many developers start their work by creating a class diagram before moving to code. Class diagrams can be informal, drawn according to a developer's personal iconography, or formal, drawn according to a diagramming specification such as Unified Modeling Language (UML) (see http://www.uml.org).

gModeler, an online Flash application written by Grant Skinner, creates class diagrams from which code and documentation can be exported. See http://www.gmodeler.com.


Just as we design our own class hierarchies for our OOP applications, ActionScript also organizes its built-in classes according to a hierarchy. In fact, every class in ActionScript (both built-in and custom) inherits directly or indirectly from the root of the built-in hierarchy: Object . The Object class defines some very basic methods that all classes can use. For example, any class can use the Object.toString( ) method, which returns a string representation of an object.

6.1.1 Class Method and Class Property Inheritance

A subclass inherits its superclass's instance methods and properties, plus its class methods and properties (i.e., those defined with the static attribute). For example, in the following code, we define a static method, s( ) , in the class A . The method s( ) is inherited by A 's subclass, B .

 class A {   public static function s ( ):Void {     trace("A.s( ) was called.");   } } class B extends A {   // For this example, class   B   does not    // define any of its own methods or properties. } 

An inherited class method or property can be accessed via the superclass, as in:

 A.s( );  // Displays: A.s( ) was called. 

or synonymously via the subclass, as in:

 B.s( );  // Also displays: A.s( ) was called. 

Within the body of either class, the method or property can be referred to directly without the qualifying class name , as in s( ) rather than A.s( ) or B.s( ) . However, it's generally wise to include the class name when referring to static methods or properties. We'll learn why later, under "Member Access from Inherited, Overriding, and Overridden Instance Methods."

The class methods and properties of some built-in classes (e.g., Math ) are not inherited by their subclasses. The cause and workaround for this problem are discussed later in this chapter under "Subclassing Built-in Classes."


Note that a bug in ActionScript 2.0 prevents access to inherited static properties before the superclass that defines the property is used in a script. For details, see "Subclasses and class properties" in Chapter 4.

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net