MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 302

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

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
statement. For
super
more information on overriding, see
"Overriding methods and properties" on page
306.
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()
, and
. You could even extend a subclass, such as Manager, and create a
raise()
promote()
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
keyword to establish inheritance between a class and its
extends
superclass, or to extend an interface. For more information on using the
keyword,
extends
see
"About writing subclasses in Flash" on page 303
and
"About writing a subclass"
keyword, see extends statement
on page
303. For additional information on the
extends
in the ActionScript 2.0 Language Reference.
302
Inheritance

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

This manual is also suitable for:

Flash 8

Table of Contents