What is Software? | What is Education? | What is Technology? | What is Research? | What is Wi-Fi? | What is Communication? | What is Customer Service?

Free SEO Tutorials & Help

Tutorials:   Adobe Flash   Adobe Photoshop   CorelDRAW   SEO  

Site Map

Website Development | Cable & Broadband Magazine | Solutions for Higher Education | Auction Anything Online

Adobe Flash Help & Tutorials

 Back to Topics

 

Overview on Interfaces in Flash

About the interface keyword

About naming interfaces

Defining and implementing interfaces

Creating interfaces as data types

Understanding inheritance and interfaces

Example: Using interfaces

Example: Creating a complex interface

 
About the interface keyword

TOP

The interface keyword defines an interface. An interface is similar to a class, with the following important differences:

  • Interfaces contain only declarations of methods, not their implementation. That is, every class that implements an interface must provide an implementation for each method declared in the interface.

  • Only public members are allowed in an interface definition; static and class members are not permitted.

  • The get and set statements are not allowed in interface definitions.

  • To use the interface keyword, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash tab of your FLA file's Publish Settings dialog box.

The interface keyword is supported only when used in external script files, not in scripts that you write in the Actions panel.

 

About naming interfaces

TOP

Interface names have an uppercase first letter, the same as class names. Interface names are usually adjectives, such as Printable. The following interface name, IEmployeeRecords, uses an initial uppercase letter and concatenated words with mixed case:

interface IEmployeeRecords {}

NOTE : Some developers start interface names with an uppercase "I" to distinguish them from classes. This is a good practice to adopt because it lets you quickly distinguish between interfaces and regular classes.

 

Defining and implementing interfaces

TOP

The process for creating an interface is the same as for creating a class. Like classes, you can define interfaces only in external ActionScript files. At a minimum, the workflow for creating an interface involves the following steps:

  • Defining a interface in an external ActionScript file

  • Saving the interface file to a designated classpath directory (a location where Flash looks for classes) or in the same directory as the application's FLA file

  • Creating an instance of the class in another script, either in a Flash (FLA) document or an external script file, or subinterfaces based on the original interface

  • Creating a class that implements the interface in an external script file

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, as shown in the following example:

interface IEmployeeRecords {

// interface method declarations

}

An interface can contain only method (function) declarations, including parameters, parameter types, and function return types.

For example, the following code declares an interface named IMyInterface that contains two methods, method1() and method2(). The first method, method1(), has no parameters and specifies a return type of Void (meaning that it does not return a value). The second method, method2(), has a single parameter of type String, and specifies a return type of Boolean.

To create a simple interface:

  1. Create a new ActionScript file and save it as IMyInterface.as.

  2. Type the following ActionScript code into the Script window:

    interface IMyInterface {

    public function method1():Void;

    public function method2(param:String):Boolean;

    }

  3. Save your changes to the ActionScript file.

    In order to use the interface within an application, you first need to create a class that implements your new interface.

  4. Create a new ActionScript file and save it as MyClass.as in the same directory as the IMyInterface.as.

  5. In the MyClass class file, type the following ActionScript code into the Script window:

    class MyClass {

    }

    In order to instruct the custom class (MyClass) to use your interface (IMyInterface), you need to use the implements keyword, which specifies that a class must define all the methods declared in the interface (or interfaces) that you implement.

  6. Modify the ActionScript code in MyClass.as (add the boldface code) so it matches the following snippet:

    class MyClass implements IMyInterface {

    }

    You place the implements keyword after the class name.

  7. Click the Check Syntax button.

    Flash displays an error in the Output panel stating that MyClass must implement method X from interface IMyInterface. You see this error message because any class that extends an interface must define each method that's listed in the interface document.

  8. Modify the MyClass document again (add the boldface code), and write ActionScript code for the method1() and method2() methods, as shown in the following snippet:

    class MyClass implements IMyInterface {

    public function method1():Void {

    // ...

    };

    public function method2(param:String):Boolean {

    // ...

    return true;

    }

    }

  9. Save the MyClass.as document and click Check Syntax.

    The Output panel no longer displays any error messages or warnings because you have now defined the two methods.

The class file that you create is not limited to the public methods that you define in the interface file. The interface file only outlines the minimum methods that you must implement, as well as those methods' properties and return types. Classes that implement a particular interface almost always include additional methods, variables, and getter and setter methods.

Interface files cannot contain any variable declarations or assignments. Functions that you declare in an interface cannot contain curly braces. For example, the following interface does not compile:

interface IBadInterface {

// Compiler error. Variable declarations not allowed in interfaces.

public var illegalVar:String;

// Compiler error. Function bodies not allowed in interfaces.

public function illegalMethod():Void {

}

// Compiler error. Private methods are not allowed in interfaces.

private function illegalPrivateMethod():Void;

// Compiler error. Getters/setters are not allowed in interfaces.

public function get illegalGetter():String;

}

 

Creating interfaces as data types

TOP

Like a class, an interface defines a new data type. You can consider any class that implements an interface to be of the type that is defined by the interface. This is useful for determining whether a given object implements a given interface. For example, consider the interface IMovable, which you create in the following example.

To create an interface as a data type:

  1. Create a new ActionScript document and save it to your hard disk as IMovable.as.

  2. In IMovable.as, type the following ActionScript code into the Script window:

    interface IMovable {

    public function moveUp():Void;

    public function moveDown():Void;

    }

  3. Save your changes to the ActionScript file.

  4. Create a new ActionScript document and save it as Box.as in the same directory as IMovable.as.

    In this document, you create a Box class that implements the IMovable interface that you created in an earlier step.

  5. In Box.as, type the following ActionScript code into the Script window:

    class Box implements IMovable {

    public var xPos:Number;

    public var yPos:Number;

    public function Box() {

    }

    public function moveUp():Void {

    trace("moving up");

    // method definition

    }

    public function moveDown():Void {

    trace("moving down");

    // method definition

    }

    }

  6. Save your changes to the ActionScript document.

  7. Create a new Flash document named boxTest.fla, and then save it in the same directory as the two previous ActionScript documents.

  8. Select Frame 1 of the Timeline, open the ActionScript editor, and then type the following ActionScript code into the Actions panel (or Script window):

    var newBox:Box = new Box();

    This ActionScript code creates an instance of the Box class, which you declare as a variable of the Box type.

  9. Save your changes to the Flash document, and then select Control > Test Movie to test the SWF file.

    In Flash Player 7 and later, you can cast an expression to an interface type or other data type at runtime. Unlike Java interfaces, ActionScript interfaces exist at runtime, which allows type casting. If the expression is an object that implements the interface or has a superclass that implements the interface, the object is returned. Otherwise, null is returned. This is useful if you want to ensure that a particular object implements a certain interface. For more information on type casting, see About casting objects.

  10. Add the following code at the end of the ActionScript code in boxTest.fla:

    if (IMovable(newBox) != null) {

    newBox.moveUp();

    } else {

    trace("box instance is not movable");

    }

    This ActionScript code checks whether the newBox instance implements the IMovable interface before you call the moveUp() method on the object.

  11. Save the Flash document, and then select Control > Test Movie to test the SWF file.

    Because the Box instance implements the IMovable interface, the Box.moveUp() method is called, and the text "moving up" appears in the Output panel.

 

Understanding inheritance and interfaces

TOP

You can use the extends keyword to create subclasses of an interface. This can be very useful in larger projects for which you might want to extend (or subclass) an existing interface and add additional methods. These methods must be defined by any classes implementing that interface.

One consideration you need to make when extending interfaces is that you receive error messages in Flash if multiple interface files declare functions with the same names but have different parameters or return types.

The following example demonstrates how you can subclass an interface file using the extends keyword.

To extend an interface:

  1. Create a new ActionScript file, and then save it as Ia.as.

  2. In Ia.as, type the following ActionScript code into the Script window:

    Interface Ia {

    public function f1():Void;

    public function f2():Void;

    }

  3. Save your changes to the ActionScript file.

  4. Create a new ActionScript file and save it as Ib.as in the same folder as the Ia.as file you created in step 1.

  5. In Ib.as, type the following ActionScript code into the Script window:

    interface Ib extends Ia {

    public function f8():Void;

    public function f9():Void;

    }

  6. Save your changes to the ActionScript file.

  7. Create a new ActionScript file and save it as ClassA.as in the same directory as the two previous files.

  8. In ClassA.as, type the following ActionScript code into the Script window:

    class ClassA implements Ib {

    // f1() and f2() are defined in interface Ia.

    public function f1():Void {

    }

    public function f2():Void {

    }

    // f8() and f9() are defined in interface Ib, which extends Ia.

    public function f8():Void {

    }

    public function f9():Void {

    }

    }

  9. Save your class file and click the Check Syntax button above the Script window.

    Flash doesn't generate any error messages as long as all four methods are defined and match the definitions from their respective interface files.

    NOTE : Classes are only able to extend one class in ActionScript 2.0, although you can use classes to implement as many interfaces as you want.

If you want your ClassA class to implement multiple interfaces in the previous example, you would simply separate the interfaces with commas. Or, if you had a class that extended a superclass and implemented multiple interfaces, you would use code similar to the following:

class ClassA extends ClassB implements Ib, Ic, Id {...}.

 

Example: Using interfaces

TOP

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

 

Example: Creating a complex interface

TOP

The following example shows several ways to define and implement interfaces. In this tutorial you learn how to create a simple interface file and how to write a class that implements multiple interfaces, as well as how to have interfaces extend other interfaces to create more complex data structures.

To create a complex interface:

  1. Create a new ActionScript document and save it as InterfaceA.as.

  2. Create a new folder called complexInterface and save InterfaceA.as to this directory.

    You save all of the files you create for this tutorial in this directory.

  3. In Interface.as, type the following ActionScript code into the Script window:

    // filename: InterfaceA.as

    interface InterfaceA {

    public function k():Number;

    public function n(z:Number):Number;

    }

  4. Save the ActionScript document and then create a new ActionScript document named ClassB.as and save it in the complexInterface directory.

    ClassB.as implements the InterfaceA interface you created previously.

  5. In ClassB.as, type the following ActionScript code into the Script window:

    // filename: ClassB.as

    class ClassB implements InterfaceA {

    public function k():Number {

    return 25;

    }

    public function n(z:Number):Number {

    return (z + 5);

    }

    }

  6. Save your changes to the ClassB.as document and then create a new Flash document and save it as classbTest.fla in the complexInterface directory.

    This class file tests the ClassB class you created previously.

  7. In classbTest.fla, type the following ActionScript code on Frame 1 of the Timeline:

    // filename: classbTest.fla

    import ClassB;

    var myB:ClassB = new ClassB();

    trace(myB.k()); // 25

    trace(myB.n(7)); // 12

  8. Save your changes to the Flash document, and then select Control >Test Movie to test the Flash document.

    The Output panel displays two numbers, 25 and 12, which are the results of the k() and n() methods in the ClassB class.

  9. Create a new ActionScript file and save it as ClassC.as in the complexInterface directory.

    This class file implements the InterfaceA interface that you created in step 1.

  10. In ClassC.as, type the following ActionScript code into the Script window:

    // filename: ClassC.as

    class ClassC implements InterfaceA {

    public function k():Number {

    return 25;

    }

    // **Error** The class must also implement method 'n' from interface 'InterfaceA'.

    }

    If you click the Check Syntax button for the ClassC class file, Flash displays an error message in the Output panel that says the current class must implement the n() method defined in the InterfaceA interface. When you create classes that implement an interface, it is important that you define methods for each entry in the interface.

  11. Create a new ActionScript document and save it as InterfaceB.as in the complexInterface directory.

  12. In InterfaceB.as, type the following ActionScript code into the Script window:

    // filename: InterfaceB.as

    interface InterfaceB {

    public function o():Void;

    }

  13. Save your changes to the InterfaceB.as document, and then create a new ActionScript document and save it in the complexInterface directory as ClassD.as.

    This class implements both the InterfaceA interface and the InterfaceB interface you created in earlier steps. The ClassD class must include method implementations for each of the methods listed in each of the interface files.

  14. In ClassD.as, type the following ActionScript code into the Script window:

    // filename: ClassD.as

    class ClassD implements InterfaceA, InterfaceB {

    public function k():Number {

    return 15;

    }

    public function n(z:Number):Number {

    return (z * z);

    }

    public function o():Void {

    trace("o");

    }

    }

  15. Save your changes to the ClassD.as file, and then create a new Flash document and save it as classdTest.fla.

    This Flash document tests the ClassD class that you created previously.

  16. In classdTest.fla, add the following ActionScript code on Frame 1 of the Timeline:

    // filename: classdTest.fla

    import ClassD;

    var myD:ClassD = new ClassD();

    trace(myD.k()); // 15

    trace(myD.n(7)); // 49

    myD.o(); // o

  17. Save your changes to the classdTest.fla file and then select Control > Test Movie to test the file.

    The values 15 and 49 and the letter o should be displayed in the Output panel. These values are the results of the ClassD.k() method, ClassD.n(), and ClassD.o() methods, respectively.

  18. Create a new ActionScript document and save it as InterfaceC.as.

    This interface extends the InterfaceA interface you created earlier, and it adds a new method definition.

  19. In InterfaceC.as, type the following ActionScript code into the Script window:

    // filename: InterfaceC.as

    interface InterfaceC extends InterfaceA {

    public function p():Void;

    }

  20. Save your changes to the ActionScript file and then create a new ActionScript file and save it as ClassE.as in the complexInterface directory.

    This class implements two interfaces, InterfaceB and InterfaceC.

  21. In ClassE.as, type the following ActionScript code into the Script window:

    // filename: ClassE.as

    class ClassE implements InterfaceB, InterfaceC {

    public function k():Number {

    return 15;

    }

    public function n(z:Number):Number {

    return (z + 5);

    }

    public function o():Void {

    trace("o");

    }

    public function p():Void {

    trace("p");

    }

    }

  22. Save your changes to the ActionScript document, and then create a new Flash document and save it as classeTest.fla in the complexInterface directory.

  23. In classeTest.fla, type the following ActionScript code on Frame 1 of the Timeline:

    // filename: classeTest.fla

    import ClassE;

    var myE:ClassE = new ClassE();

    trace(myE.k()); // 15

    trace(myE.n(7)); // 12

    myE.o(); // o

    myE.p(); // p

  24. Save the Flash document, and then select Control > Test Movie to test the SWF file.

    The values 15, 12, o, and p display in the Output panel. These values are the values that return from the ClassE.k(), ClassE.n(), ClassE.o(), and ClassE.p() methods. Since the ClassE class implemented both the InterfaceB and InterfaceC interfaces, each method from the two interface files must be defined. Although the InterfaceB and InterfaceC interfaces only define the o() and p() methods, InterfaceC extends InterfaceA. This means that all of its defined methods, k() and n(), must also be implemented.

     

Copyright ADOBE - All Rights Reserved Worldwide

 

 

More Topics:

Working with Flash Documents

How to work in Flash WorkSpace

Working with Projects in Flash

Process to Build your First Application in Flash

Using Symbols, Instances and Library Assets in Flash

How to Build Video Player in Flash

How to Work with Color, Strokes and Fills in Flash

How to Create Document in Flash

What is Vector and Bitmap Graphics in Flash

How to Create a Banner in Flash, Part 1

How to Work with Text in Flash

How to Create a Banner in Flash, Part 2

How to Use Imported Artwork in Flash

How to Create a Banner in Flash, Part 3

How to Work with Graphic Objects in Flash

How to Work with Layers in Flash

How to Use Filters and Blends

Working with Graphics in Flash

What is Accessibility Features in Flash

How to Create Motion (Shape Tween & Motion Tween) in Flash

How to Create an Application in Flash

What is Masking in Flash

How to Work with Video in Flash

How to Use Layout Tools in Flash

What are Behaviors in Flash

How to Work with Sound in Flash

How to Create Symbols and Instances in Flash

What is ActionScript in Flash

How to Write ActionScript With Script Assist in Flash

How to Add Button Animation and Navigation in Flash

What is Data Integration in Flash

How to Work with Screens

How to Create a Presentation with Screens

What is Extending Flash

How to Create Multilanguage Text in Flash

How to Create Graphics: Draw in Flash

What is Flash Lite

Ways of Data Integration

How to Create Graphics: Create a Timeline Animation in Flash

Getting Started with Flash Lite in Flash

How to Publish Flash Documents

How to Create Graphics: Making Animations with Easing

Learning Flash Lite 1.X ActionScript in Flash

How to Export Flash Content and Images from Flash

How to Create Graphics: Applying Gradients in Flash

Process of Writing and Editing ActionScript 2.0 in Flash

How to Create Accessible Content in Flash

How to Create Graphics: Apply Graphic Filters and Blends

What is Data and Data Types in Flash

Process of Printing from SWF Files in Flash

Using ActionScript: How to Use Script Assist mode in Flash

Learn Syntax and Language Fundamentals in Flash

How to Create E-learning Content in Flash

Using ActionScript: How to Write Scripts in Flash

Working with Functions and Methods in Flash

Process of Using Templates in Flash

Using ActionScript: Process of Adding Interactivity in Flash

What are Classes in Flash

Control Tag Summary of XML to UI in Flash

Using ActionScript: How to Create a Form with Conditional Logic and Send Data in Flash

What is Inheritance in Flash

What is Data Integration: Overview

Using ActionScript: How to Work with Objects and Classes in Flash

Overview on Interfaces in Flash

What is Data Integration: Using XML for a Timesheet

How to Work with Text and Strings in Flash

How to use Handling Events in Flash

What is Data Integration: Using XUpdate to Update the Timesheet

Learning Animation, Filters and Drawings in Flash

How to Work with Movie Clips in Flash

How to Create Interaction with ActionScript in Flash

How to Work with Images, Sound, and Video in Flash

How to Work with External Data in Flash

What is Security in Flash

How to Debug Applications in Flash

List of Error Messages in Flash

Using Object-Oriented Programming with ActionScript 1.0 in Flash

How to Write Scripts for Earlier Versions of Flash Player in Flash

List of all Keyboard Keys and Key Code Values for using in Flash

Terminology

Introduction to Components in Flash

What are Components in Flash

How to Create an Application with Components

How to Work with Components in Flash

How to Handle Component Events in Flash

How to Customize Components in Flash

How to Create Components in Flash

What is Collection Properties in Flash