About inheritance

In Classes you saw how you could create a class file to create your own custom data types. Learning how to create custom class files shows you how to move code off the timeline and into external files. Moving code into external files makes it easier to edit your code. Now that you're familiar with the basics of creating your own custom classes, you learn about an object-oriented programming (OOP) technique called subclassing or extending a class, which lets you create new classes based on an existing class.

One of the benefits of OOP is that you can create subclasses of a class. The subclass inherits all the properties and methods of a superclass. For example, if you extend (or subclass) the MovieClip class, you are creating a custom class that extends the MovieClip class. Your subclass inherits all of the properties and methods of the MovieClip class. Or you might create a set of classes that extends from a custom superclass. For example, the Lettuce class might extend from the Vegetable superclass.

Your subclass typically defines additional methods and properties that you can use in your application, hence it extends the superclass. Subclasses can also override (provide their own definitions for) methods inherited from a superclass. If a subclass overrides a method inherited from its superclass, you can no longer access the superclass's definition within the subclass. The only exception to the above rule is that, if you are within the subclass's constructor function, you call the superclass's constructor using the super statement. For more information on overriding, see Overriding methods and properties.

For example, you might create a Mammal class that defines certain properties and behaviors that are common to all mammals. You could then create a Cat subclass that extends the Mammal class. Using subclasses lets you reuse code so that instead of re-creating all the code common to both classes you could simply extend an existing class. Another subclass, the Siamese class, could extend the Cat class, and so on. In a complex application, determining how to structure the hierarchy of your classes is a large part of the design process.

Inheritance and subclassing are very useful in larger applications, because they let you create a series of related classes that can share functionality. For example, you could create an Employee class that defines the basic methods and properties of a typical employee within a company. You could then create a new class called Contractor that extends the Employee class and inherits all of its methods and properties. The Contractor class could add its own specific methods and properties, or it could override methods and properties that are defined in the Employee superclass. You could then create a new class called Manager, which also extends the Employee class and defines additional methods and properties such as hire(), fire(), raise(), and promote(). You could even extend a subclass, such as Manager, and create a new class called Director, which again adds new methods or overrides existing methods.

Each time that you extend an existing class, the new class inherits all the current methods and properties of the subclass. If each class wasn't related, you'd have to rewrite each method and property in each separate class file, even if the functionality was the same in the fellow classes. You would have to spend a lot more time not only coding, but also debugging your application and maintaining a project if similar logic changed in multiple files.

In ActionScript, you use the extends keyword to establish inheritance between a class and its superclass, or to extend an interface. For more information on using the extends keyword, see About writing subclasses in Flash and About writing a subclass. For additional information on the extends keyword, see extends statement in the ActionScript 2.0 Language Reference.


Flash CS3