Interface is a class used as a pattern, or template for classes with similar functions, that must respect a certain basic structure.
Synthesized, the Interface is a class with a list of required methods that must be created in the classes where it is implemented.
All the methods specified in the "Interface" must be defined in the classes where it's applied, having the same name, data type and parameters as indicated in the "Interface".

Creating an interface

The interface class is created in the same way as other types of classes, in a "package", saved in a separate AS document. The difference is that, instead of the keyword "class" you use the keyword "interface"; also, in its body you only write a list of methods, without code instructions.
The general syntax is:

package {
  public interface InterfaceName {
    function methodName(prop1:DataType, prop2:DataType, ...):DataType;
    function otherMethod(prop1:DataType, prop2:DataType, ...):DataType;
- When declaring the methods in an interface, their code or curly brackets are not added, neither an attribute, because in an interface class are added only the methods that will have the "public" attribute.
- Properties can not be specified.

Here's an example of an interface, named "ITest", in which we specify 2 methods: "Links()" and "Tutorials()".
package {
  // Create interface
  public interface ITest {
    // A list with required methods
    function Links():void;
    function Tutorials(gen:String, nota:int):String
- This code must be added in an "ActionScript 3.0 Class" document (or "ActionScript 3.0 Interface"), write "ITest" at the class name and the file with this code must be saved with the name "", in the same folder as the classes which will implement this interface.

Implementing interface

Once the "interface" was saved, you can create classes that implement the methods specified in the interface.
To implement an interface, use the keyword implements and the interface name after the class name:

public class ClassName implements InterfaceName {
  // Instructions
- "ClassName" must contain all the methods defined in "InterfaceName", with the "public" attribute, the number of parameters and their DataType established in the interface. The class can also contain other methods.

In the following example, we create a class (named webDevelopment), in a new AS document, with the name "" and save it in the same folder as the "ITest" interface.
package {
  // Create class that implements the ITest interface
  public class webDevelopment implements ITest
    // Define a protected property
    protected var site:String = '';

          /* Define the required methods (Links and Tutorials), specified in interface */

    // Returns in Output the value of the 'site' property
    public function Links():void

    // Return the value of a variable that takes the transmitted arguments
    public function Tutorials(gen:String, note:int):String
      var re:String = gen+'-'+note;
      return re;

    // creates an additional method
    // Displays in Output the argument received
    public function Diverse(val:*):void
- The required methods ("Links()" and "Tutorials()") respect the exact parameters (number, order, data type) set in the "ITest" interface. Other methods (in this case "Diverse") and properties are optional, depending on each class role.
- In the required methods, the name of the parameters does not matter, but the number, data-type and order must be the same as in "interface".

To test this example, open a new Flash document, add in the "Actions panel" the following ActionScript code, than save it in the same directory as the interface and class above.
// Create an object instance of webDevelopment class
var obj:webDevelopment = new webDevelopment();

// Call the methods
obj.Links();                             //
trace(obj.Tutorials('Flash', 8));        // Flash-8
obj.Diverse(2010);                       // 2010
- Because the "webDevelopment" class meets the conditions specified in the implemented interface (ITest), the script is functional.
- If any of the conditions are not met in the class: such as not defining a method, adding an extra parameter, or declaring with other data-type; the script will return an error.
Therefore, implementing an "interface" is usefull especially when you want to create multiple classes with similar roles, and you want them to have a minimal order and structure of methods, easier to remember.

Interface as a data type

The interface can also be applied as a data type to variables (or function parameters).
Then, the variable (or parameter) can represent an instance of the class which implements that "interface".
You can understand it better from the following example, where we create another class (called WebProgramming) that implements the interface "ITest".

package {
  // Create class that applies ITest interface
  public class WebProgramming implements ITest
    // Defining a protected property
    protected var adr:String = '';

          /* Defining the required methods (Links and Tutorials), specified in interface */

    // Returns a string in Output
    public function Links():void
      trace('Have a good life');

    // Returns the value of a variable "re"
    public function Tutorials(gen:String, nr:int):String
      var re:String = nr+'-'+ this.adr+gen;
      return re;
In the FLA document, write the following code in the "Actions panel".
// Create object instaces of the classes which implements 'ITest'
var web_development:webDevelopment = new webDevelopment();
var web_programming:WebProgramming = new WebProgramming();

// Create a function with a parameter that has "ITest" interface as data type
function courses(cls:ITest):void
  // Call the methods set in ITest, through the "cls" parameter
  trace(cls.Tutorials('flash', 4));

// Calls the courses() function, passing for parameter the instances created above
courses(web_development);        // In Output displays: "" and "flash-4"
courses(web_programming);        // In Output displays: "Have a good life" and ""
- Notice that calling the function with different arguments, reprezenting the class instances, the function uses the parameter "cls" as instance of the class (because it has "ITest" as data type), and can call the same methods ("Links" and "Tutorials") for each argument, because these methods are specified in "interface", so they must exist in each class that implements that interface, with the same type of parameters.
- This technique helps you to not create the same function for each instance.

• You can also create child-interface that inherits the list of methods specified in a parent-interface, adding also additional methods if you want.
The syntax to define a child-interface is:
public interface ChildInterface extends ParentInterface {
  // List of additional methods
- The "ChildInterface" will contain the list of methods inherited from the "ParentInterface", and whatever else is added in it. It will not affect the ParentInterface.

• An "interface" can also be applied to child classes, using this syntax:
public ChildClass extends ParentClass implements Interface
- "ChildClass" can be an extension of any class, doesn't matter if the "ParentClass" implements or not an Interface.

• A class can implement multiple interfaces, separated by comma, like in this syntax:
public class ClassName implements Interface1, Interface2

If you modify an Interface which is already applied to a class, that class will not work.

- The FLA and AS files with examples from this lesson can be downloaded from: Classes - Interface in ActionScript 3.

Daily Test with Code Example

Click on the tag that creates an ordered list
<ul> <tr> <ol>
Which selector represents the ID of an element in CSS
.name #name name
#id {
  color: #0110fb;
What statement creates an object in JavaScript?
{} [] new Date()
var obj = {"site": "", "pr": 5};
Indicate the instruction used to evaluate if a condiition is True or False
else if() switch()
$var = 8;
if($var == 8) echo $var;
Classes - Interface in ActionScript 3

Last accessed pages

  1. Drag Racer V3 (948)
  2. The Number and Math Objects (344)
  3. Download JavaScript resources (2345)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33344)
  5. Recursive function to create Multi-Level Menu in PHP (4811)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (49682)
  2. Read Excel file data in PHP - PhpExcelReader (38072)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (37003)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33344)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (30742)