Example: Using interfaces

In this example you create a simple interface that you can reuse between many different classes.

To build an interface:

  1. Create a new ActionScript file and save it as IDocumentation.as.
  2. In IDocumentation.as, type the following ActionScript code into the Script window:
    interface IDocumentation {
        public function downloadUpdates():Void;
        public function checkForUpdates():Boolean;
        public function searchHelp(keyword:String):Array;
    }
    
  3. Save the changes that you made to the ActionScript interface file.
  4. Create a new ActionScript file in the same directory as the IDocumentation.as file, and save this new file as FlashPaper.as.
  5. In FlashPaper.as, type the following ActionScript code into the Script window:
    class FlashPaper implements IDocumentation {
    }
    
  6. Save the changes that you made to the ActionScript file.
  7. Click the Check Syntax button for your ActionScript class.

    You see an error that's similar to the following message:

    **Error** path\FlashPaper.as: Line 1: The class must implement method 'checkForUpdates' from interface 'IDocumentation'.
         class FlashPaper implements IDocumentation {
    Total ActionScript Errors: 1      Reported Errors: 1
    

    This error appears because the current FlashPaper class doesn't define any of the public methods that you defined in the IDocumentation interface.

  8. Open the FlashPaper.as class file again and modify the existing ActionScript code so that it matches the following code:
    class FlashPaper implements IDocumentation {
        private static var __version:String = "1,2,3,4";
        public function downloadUpdates():Void {
        };
        public function checkForUpdates():Boolean {
            return true;
        };
        public function searchHelp(keyword:String):Array {
            return []
        };
    }
    
  9. Save your changes to the ActionScript file, and then click Check Syntax again.

    This time you don't see any errors appear in the Output panel.

    NOTE

    You can add as many additional static, public, or private variables or methods as you want to the FlashPaper class file. The interface file defines only a set of minimum methods that must appear within any class that implements that interface.

  10. Open the IDocumentation interface document again, and add the following boldface line of code (below the searchHelp() method):
    interface IDocumentation {
        public function downloadUpdates():Void;
        public function checkForUpdates():Boolean;
        public function searchHelp(keyword:String):Array;
        public function addComment(username:String, comment:String):Void;
    }
    
  11. Save your changes to the interface file, and then reopen the FlashPaper.as document.
  12. Click the Check Syntax button, and you see a new error message in the Output panel:
    **Error** path\FlashPaper.as: Line 1: The class must implement method 'addComment' from interface 'IDocumentation'.
         class FlashPaper implements IDocumentation {
    Total ActionScript Errors: 1      Reported Errors: 1
    

    You see the previous error because the FlashPaper.as class file no longer defines all the classes that you outlined in the interface file. To fix this error message, you must either add the addComment() method to the FlashPaper class or remove the method definition from the IDocumentation interface file.

  13. Add the following method in the FlashPaper class:
    public function addComment(username:String, comment:String):Void {
        /* Send parameters to server-side page, which inserts comment into database. */
    }
    
  14. Save the changes to FlashPaper.as and click the Check Syntax button and you should no longer receive any errors.

In the previous section, you created a class-based on the IDocumentation interface file. In this section you create a new class that also implements the IDocumentation interface, although it adds some additional methods and properties.

This tutorial demonstrates the usefulness of using interfaces because if you want to create another class that extends the IDocumentation interface, you can easily identify the methods that are required within the new class.