# Creating and using interfaces

An interface in object-oriented programming is like a class, but one whose methods have only been declared, but otherwise don't "do" anything. That is, an interface consists "empty" methods. You might wonder why this is useful.

One use of interfaces is to enforce a protocol between otherwise unrelated classes, as discussed next. For example, say you're part of a team of programmers, each of who 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 some common way for the different classes to communicate. That is, you need to define an interface, or communication protocol, that all the classes must adhere to.

One way to do this would be to create a Communication class that defines all of these methods, and then have each class extend, or inherit from, this superclass. But since the application consists of classes that are unrelated, it doesn't make sense to force 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—that is, provide their own definitions—for those methods.

You can usually do what you need without interfaces but when used appropriately, they can make the design of your applications more elegant, scalable, and maintainable.

## Creating an interface

The process for creating an interface is the same as for creating a class. Like classes, you can only define interfaces in external ActionScript (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.

    interface interfaceName {
    	// interface method declarations
    }

Interface may only contain function declarations, including parameters, parameter types, and function return types. For instance, the following code declares an interface named MyInterface that contains two methods `method_1()` and `method_2()`. The first method declaration takes no parameters and has no return type (specified as `Void`). The second method declaration takes a single parameter of type String, and specifies a return type of Boolean.

    interface MyInterface {
    	function method_1():Void;
    	function method_2(param:String):Boolean;
    }

Interfaces may not contain any variable declarations or assignments. Functions declared in an interface cannot contain curly braces that are typically used to define function bodies. For example, the following interface won't compile.

    interface MalFormedInterface {
    	var illegalVar;
    	// Compiler error. Variable declarations not allowed in interfaces.

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

}

## Using an interface

You can think of the function declaration in an interface as a list of services, which another class agrees to do the work for. An interfaces's functions don't actually do anything; that job is left to the class or classes that implement that interface.