Defining Interfaces

Classes have both an interface and an implementation. The interface is the way in which the outside world can communicate with an object. The interface simply says what methods are publicly available, what parameters those methods accept, and what, if any, types are returned by methods. The interface does not say anything at all about what a method does or how it does it.

ActionScript defines a formal interface construct that you can use to define interfaces. The syntax for defining an interface is very similar to that for defining a class, but it is simplified because it does not require (nor allow) you to provide any implementation. An interface construct defines only the interface. An interface consists of methods, including getters and setters. The basic syntax is as follows:

package package {
   public interface Interface {
      function method(parameter:Type):ReturnType;
      function get property():ReturnType;
      function set property(value:Type):void;
   }
}

The major differences between the syntax for a class and the syntax for an interface are that an interface uses the interface keyword rather than the class keyword and a semicolon appears after the return type for each method, instead of a code block defining the function body. Additionally, interfaces can describe only the public methods for implementing classes. That means that there is no need for a public/private/protected/internal modifier, and interfaces do not allow those modifiers.

By convention, interface names start with I. For example IExample indicates that the type is an interface.

The following is an example of an interface called IExample:

package {
   public interface IExample {
      function sampleMethod(parameter1:String,
              parameter2:uint):void;
   }
}

When you want to define a class that implements the interface, you use the implements keyword following the name of the class. For example, the following defines class A as implementing an interface called IExample:

public class A implements IExample {

If the class extends a superclass, then the implements keyword follows the name of the superclass:

public class A extends SuperClass implements IExample {

When a class implements an interface, it essentially signs a contract that it will implement the methods defined in the interface. If the implementing class does not define the necessary methods with exactly the same signatures as described in the interface, the compiler throws an error. For example, the following class says it implements IExample, but it does not declare the necessary method sampleMethod():

package {
   public class A implements IExample {
      public function A() {}
   }
}

Because A does not correctly define the necessary method, the compiler will throw an error. Also, if A is defined as follows, the compiler will still throw an error because although A defines sampleMethod(), it does not use the correct signature:

package {
   public class A implements IExample {
      public function A() {}
      public function sampleMethod(parameter1:String):void {
         trace("sampleMethod");
      }
   }
}

The compiler will approve the class only when the class correctly adheres to the contract and defines all the necessary methods with the correct signatures, as it does in this example:

package {
   public class A implements IExample {
      public function A() {}
      public function sampleMethod(parameter1:String,
         parameter2:uint):void {
         trace("sampleMethod");
      }
   }
}

It’s also important to note that, unlike inheritance, you can implement more than one interface per class. To implement more than one interface, simply use a comma-delimited list. For example, the following code defines A so that it implements IExample and ISample:

public class A implements IExample, ISample {

Note that a class signs a contract for each interface it implements. It must implement all the methods for all the interfaces it implements.

 

Post A Comment

Anti-Spam Quiz: