Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: Adobe Flex

Adobe Flex: Article

Ask the Expert

Basics of Flex explained

These classes extend the base functionality of the Automobile class, and therefore are instances of the Automobile class. If we have a function outside of the Automobile class, which takes an automobile as the parameter, both a SportsCar and Truck will work since they are both Automobiles. We could have a function such as the following: If we pass in a Truck Instance, and a SportsCar instance, both will work, and each will use the functionality of their specific class instead of the base Automobile class.

public function race( auto1 : Automobile, auto2 : Automobile ) : void
{
     auto1.accelerate();
     auto2.accelerate();
}

I'll get into some more fine-grain details about inheritance later. Now, let's move on to interfaces.

Interfaces are slightly different than inheritance. An interface is a set of "rules" that an object must adhere to. The "rules" are actually method signatures and variable instances that your class (which implements the interface) must implement. When we define an interface, we define method signatures that are required for classes that implement the interface. There is no actual code in an interface; it simply defines methods that must exist within your class. Your class that implements the interface must implement the code for the actual function. If you have multiple classes that implement an interface, those classes must have the same functions (only the ones required to implement the interface), but that is where the similarities of the two classes may stop. They could have completely different logic and properties within them. This is where inheritance and interfaces differ. Two objects that inherit from the same base class have a lot in common (properties and methods); two objects that implement the same interface only have those interface method signatures in common.

Let's now make an Automobile Interface that defines the functions required to create an IAutomobile object (note the "I" stands for "interface"):

public interface IAutomobile
{
     function accelerate() : void;
     function decelerate() : void;
     function turn( direction : Number ) : void;
}

We can use the IAutomobile interface to create objects (classes) that behave as Automobile objects. These classes do not necessarily inherit from each other and do not necessarily share any common properties.

public class Car implements IAutomobile
{
    private var direction:Number;
    private var speed:Number;

    public function turn(direction:Number):void
    {
       this.direction = direction;
    }

    public function decellerate():void
    {
       this.speed++;
    }

    public function accellerate():void
    {
       this.speed--;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas implements="IAutomobile"
xmlns:mx="http://www.adobe.com/2006/mxml">
   <mx:Script>
     <![CDATA[

       private var direction:Number;
       private var speed:Number;

       public function turn(direction:Number):void
       {
         this.direction = direction;
       }

       public function decellerate():void
       {
         this.speed++;
       }

       public function accellerate():void
       {
         this.speed--;
       }
     ]]>
   </mx:Script>
</mx:Canvas>=


More Stories By Andrew Trice

Andrew Trice is a consultant with Cynergy Systems in Washington, DC, where he specializes in development of Flex-based Rich Internet Applications. Andrew has over 5 years of proven experience in the RIA industry, including application design and development using Flex, Flash, ColdFusion, J2EE and .NET architectures.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.