Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Interfaces > About interfaces | |||
In object-oriented programming, interfaces are like classes whose methods are not implemented (defined)--that is, they otherwise don't "do" anything. Therefore, an interface consists of "empty" methods. Another class can then implement the methods declared by the interface. In ActionScript, the distinction between interface and object is only for compile-time error checking and language rule enforcement.
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 chaseTail() and eatCatNip(). Like 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.
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 About casting objects. 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. For a tutorial on creating an application with interfaces, see Example: Using interfaces.
For more information on using interfaces, see the following sections:
|
|
|
|