Actionscript Course

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 "ITest.as", 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 "webDevelopment.as" 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 = 'coursesweb.net';

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

    // Returns in Output the value of the 'site' property
    public function Links():void
    {
      trace(this.site);
    }

    // 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
    {
      trace(val);
    }
  }
}
- 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();                             // coursesweb.net
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 = 'coursesweb.net/';

          /* 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
  cls.Links();
  trace(cls.Tutorials('flash', 4));
}

// Calls the courses() function, passing for parameter the instances created above
courses(web_development);        // In Output displays: "coursesweb.net" and "flash-4"
courses(web_programming);        // In Output displays: "Have a good life" and "4-coursesweb.net/flash"
- 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

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Classes - Interface in ActionScript 3

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)
  2. JavaScript strip_tags and stripslashes (8825)
  3. Date and Time in ActionScript 3 (10086)
  4. Bind Tool and Control Points (4430)
  5. Clear Canvas Context (8116)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)