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

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

An interface is not a class; however, this is not altogether true in ActionScript at runtime
because an interface is abstract. ActionScript interfaces do exist at runtime to allow type
casting (changing an existing data type to a different type). The ActionScript 2.0 object model
does not support multiple inheritance. Therefore, a class can inherit from a single parent class.
This parent class can be either a core or Flash Player class or a user-defined (custom) class.
You can use interfaces to implement a limited form of multiple inheritance, by which a class
inherits from 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
and
. Like Java, ActionScript 2.0 does not
chaseTail()
eatCatNip()
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
and
methods. A Cat class, or any other class, could then
chaseTail()
eatCatNip()
implement this interface and provide definitions for those methods.
You can also think of an interface as a "programming contract" that you can use 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 class within the same
application. While designing the application, you agree on a set of methods that the different
classes use to communicate. 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 results. The interface is like a
communication protocol to which all the classes must adhere.
One way to do this would be to create a class that defines all these methods and then have
each class extend, or inherit from, this superclass. But because the application consists of
classes that are unrelated, it doesn't make sense to put them all into a common class hierarchy.
A better solution is to create an interface that declares the methods these classes use to
communicate, and then have each class implement (provide its own definitions for) those
methods.
You can usually program successfully without using interfaces. When used appropriately,
however, interfaces can make the design of your applications more elegant, scalable, and
maintainable.
ActionScript interfaces exist at runtime to allow type casting; see
Chapter 4, "About casting
objects," on page
111. An interface is not an object or a class, but the workflow is similar to
working with classes. For more information on the class workflow, see
"Writing custom class
files" on page
235. For a tutorial on creating an application with interfaces, see
"Example:
Using interfaces" on page
321.
314
Interfaces

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?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Flash 8

Table of Contents