User Guide
128 Object-Oriented Programming in ActionScript
The Flash Player API follows a convention in which interface names begin with an uppercase
I, but you can use any legal identifier as an interface name. Interface definitions are often
placed at the top level of a package. Interface definitions cannot be placed inside a class
definition or inside another interface definition.
Interfaces can extend one or more other interfaces. For example, the following interface,
IExample, extends the IExternalizable interface:
public interface IExample extends IExternalizable
{
function extra():void;
}
Any class that implements the IExample interface must include implementations not only for
the
extra() method, but also for the writeExternal() and readExternal() methods
inherited from the IExternalizable interface.
Implementing an interface in a class
A class is the only ActionScript 3.0 language element that can implement an interface. Use the
implements keyword in a class declaration to implement one or more interfaces. The
following example defines two interfaces, IAlpha and IBeta
, and a class, Alpha, that
implements them both:
interface IAlpha
{
function foo(str:String):String;
}
interface IBeta
{
function bar():void;
}
class Alpha implements IAlpha, IBeta
{
public function foo(param:String):String {}
public function bar():void {}
}
In a class that implements an interface, implemented methods must do the following:
■ Use the public access control identifier.
■ Use the same name as the interface method.
■ Have the same number of parameters, each with data types that match the interface
method parameter data types.
■ Use the same return type.