User Guide
Interfaces 127
In the Flash Player API, the EventDispatcher class implements the IEventDispatcher interface
by defining all of the IEventDispatcher interface methods and adding method bodies to each
of the methods. The following code is an excerpt from the EventDispatcher class definition:
public class EventDispatcher implements IEventDispatcher
{
function dispatchEvent(event:Event):Boolean
{
/* implementation statements */
}
...
}
The IEventDispatcher interface serves as a protocol that EventDispatcher instances use to
process event objects and pass them to other objects that have also implemented the
IEventDispatcher interface.
Another way to describe an interface is to say that it defines a data type just as a class does.
Accordingly, an interface can be used as a type annotation, just as a class can. As a data type,
an interface can also be used with operators, such as the
is and as operators, that require a
data type. Unlike a class, however, an interface cannot be instantiated. This distinction has led
many programmers to think of interfaces as abstract data types and classes as concrete data
types.
Defining an interface
The structure of an interface definition is similar to that of a class definition, except that an
interface can contain only methods with no method bodies. Interfaces cannot include
variables or constants, but can include getters and setters. To define an interface, use the
interface keyword. For example, the following interface, IExternalizable, is part of the
flash.utils package in the Flash Player API. The IExternalizable interface defines a protocol for
serializing an object, which means converting an object into a format suitable for storage on a
device or for transport across a network.
public interface IExternalizable
{
function writeExternal(output:IDataOutput):void;
function readExternal(input:IDataInput):void;
}
Note that the IExternalizable interface is declared with the public access control modifier.
Interface definitions may only be modified by the
public and internal access control
specifiers. The method declarations inside an interface definition cannot have any access
control specifiers.