User Guide

Creating and using interfaces 261
// In Shapes/Circle.as:
class Shapes.Circle {}
// In Shapes/Triangle.as:
class Shapes.Triangle {}
To reference a class that resides in a package directory, you can either specify its fully qualified
class name or import the package by using the
import statement (see the following section).
For more information about the naming conventions for packages, see “Packages” on page 73 in
Chapter 3, “Using Best Practices.
Creating and using interfaces
An interface in object-oriented programming is like a class whose methods have been declared,
but otherwise dont “do” anything. That is, an interface consists of “empty” methods.
One use of interfaces is to enforce a protocol between otherwise unrelated classes. For example,
suppose you’re part of a team of programmers, each of whom is working on a different part—that
is, a different class—of a large application. Most of these classes are unrelated, but you still need a
way for the different classes to communicate. You need to define an interface, or 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 doesnt 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 will 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.
For more information on creating and using interfaces, see the following topics:
“Creating an interface” on page 261
“Interfaces as data types” on page 262
Creating an interface
The process for creating an interface is the same as for creating a class. As with classes, you can
define interfaces only in external AS files. You declare an interface using the
interface keyword,
followed by the interface name, and then left and right curly braces ({}), which define the body of
the interface, as shown in the following example:
interface interfaceName {
// interface method declarations
}
An interface can contain only method (function) declarations, including parameters, parameter
types, and function return types.