User Guide
Principles of object-oriented programming 249
Inheritance
One of the primary benefits of object-oriented programming is that you can create subclasses of a
class; the subclass then inherits all the properties and methods of the superclass. The subclass
typically defines additional methods and properties, or extends the superclass. Subclasses can also
override (provide their own definitions for) methods inherited from a superclass.
For example, you might create a Mammal class that defines certain properties and behaviors
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 can simply extend an existing class. Another subclass, say, 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.
In ActionScript, you use the
extends keyword to establish inheritance between a class and its
superclass. For more information, see “Creating subclasses” on page 258.
Interfaces
Interfaces in object-oriented programming can be described as classes whose methods are not
implemented (defined). Another class can implement the methods declared by the interface.
An interface can also be thought of as a “programming contract” that can be used to enforce
relationships between otherwise unrelated classes. For example, suppose you are working with a
team of programmers, each of whom is working on a different part (class) of the same application.
While designing the application, you agree on a set of methods that the different classes will use
to communicate. So you create an interface that declares these methods, their parameters, and
their return types. Any class that implements this interface must provide definitions for those
methods; otherwise, a compiler error will result.
You can also use interfaces to provide a limited form of multiple inheritance, which is not allowed
in ActionScript 2.0. In multiple inheritance, a class extends more than one class. For example, in
C++, the Cat class could extend the Mammal class as well as a Playful class, which has methods
chaseTail and eatCatNip. As with Java, ActionScript 2.0 does not allow a class to extend multiple
classes directly but does allow a class to extend a single class and implement multiple interfaces.
So, you could create a Playful interface that declares the chaseTail and eatCatNip methods. A Cat
class, or any other class, could then implement this interface and provide definitions for those
methods.
Unlike Java interfaces, ActionScript interfaces exist at runtime, which allows type casting. For
more information, see “Creating an interface” on page 261.
Encapsulation
In elegant object-oriented design, objects are seen as “black boxes” that contain, or encapsulate,
functionality. A programmer should be able to interact with an object by knowing only its
properties, methods, and events (its programming interface), without knowing the details of its
implementation. This approach enables programmers to think at higher levels of abstraction and
provides an organizing framework for building complex systems.