Defining and implementing interfaces

The process for creating an interface is the same as for creating a class. Like classes, you can define interfaces only in external ActionScript files. At a minimum, the workflow for creating an interface involves the following steps:

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 IEmployeeRecords {
  // interface method declarations
}

An interface can contain only method (function) declarations, including parameters, parameter types, and function return types.

For more information on conventions for structuring classes and interfaces, see Best Practices and Coding Conventions for ActionScript 2.0. For a tutorial on creating an application that uses an interface, see Example: Using interfaces.

For example, the following code declares an interface named IMyInterface that contains two methods, method1() and method2(). The first method, method1(), has no parameters and specifies a return type of Void (meaning that it does not return a value). The second method, method2(), has a single parameter of type String, and specifies a return type of Boolean.

To create a simple interface:

  1. Create a new ActionScript file and save it as IMyInterface.as.
  2. Type the following ActionScript code into the Script window:
    interface IMyInterface {
      public function method1():Void;
      public function method2(param:String):Boolean;
    }
    
  3. Save your changes to the ActionScript file.

    In order to use the interface within an application, you first need to create a class that implements your new interface.

  4. Create a new ActionScript file and save it as MyClass.as in the same directory as the IMyInterface.as.
  5. In the MyClass class file, type the following ActionScript code into the Script window:
    class MyClass {
    }
    

    In order to instruct the custom class (MyClass) to use your interface (IMyInterface), you need to use the implements keyword, which specifies that a class must define all the methods declared in the interface (or interfaces) that you implement.

  6. Modify the ActionScript code in MyClass.as (add the boldface code) so it matches the following snippet:
    class MyClass implements IMyInterface {
    }
    

    You place the implements keyword after the class name.

  7. Click the Check Syntax button.

    Flash displays an error in the Output panel stating that MyClass must implement method X from interface IMyInterface. You see this error message because any class that extends an interface must define each method that's listed in the interface document.

  8. Modify the MyClass document again (add the boldface code), and write ActionScript code for the method1() and method2() methods, as shown in the following snippet:
    class MyClass implements IMyInterface {
        public function method1():Void {
            // ...
        };
        public function method2(param:String):Boolean {
            // ...
            return true;
        }
    }
    
  9. Save the MyClass.as document and click Check Syntax.

    The Output panel no longer displays any error messages or warnings because you have now defined the two methods.

The class file that you create is not limited to the public methods that you define in the interface file. The interface file only outlines the minimum methods that you must implement, as well as those methods' properties and return types. Classes that implement a particular interface almost always include additional methods, variables, and getter and setter methods.

Interface files cannot contain any variable declarations or assignments. Functions that you declare in an interface cannot contain curly braces. For example, the following interface does not compile:

interface IBadInterface {
    // Compiler error. Variable declarations not allowed in interfaces.
    public var illegalVar:String;

    // Compiler error. Function bodies not allowed in interfaces.
    public function illegalMethod():Void {
    }

    // Compiler error. Private methods are not allowed in interfaces.
    private function illegalPrivateMethod():Void;

    // Compiler error. Getters/setters are not allowed in interfaces.
    public function get illegalGetter():String;
}

For a tutorial demonstrating how to create a complex interface, see Example: Using interfaces.

The rules for naming interfaces and storing them in packages are the same as those for classes; see About naming class files.