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

The previous two components both implement the IAutomobile interface, but have nothing else in common. One is simply a class that implements the interface, the other is an MXML component that implements the interface. The MXML component example extends the mx:Canvas component (the same thing could be done by creating an AS class that extends mx.containers.Canvas). Now, let's look at a function similar to the "race" function from earlier:

public function race( auto1 : IAutomobile, auto2 : IAutomobile ) : void
{
     auto1.accellerate();
     auto2.accellerate();
}

This example will work with either object that I have created because both objects implement the IAutomobile interface. The do not rely upon functions in the class hierarchy, just those that were implemented for this interface. You can also use multiple interfaces on classes that you create. Implementing multiple interfaces basically means that you are adding more required method signatures to your class, and you will have to implement these methods to satisfy each interface. On the other hand, you cannot inherit from multiple classes. Some programming languages allow for multiple inheritance (http://en.wikipedia.org/wiki/Multiple_inheritance). ActionScript 3.0 does not support multiple inheritance (so I'll stop there).

Enough of this rambling...what does this have to do with Flex?

Inheritance and interfaces are used extensively in AS3 to create the Flex framework. Just look at the Flex documentation for the mx:Canvas component (http://livedocs.macromedia.com/flex/2/langref/mx/containers/Canvas.html) and you can see inheritance in action (http://livedocs.macromedia.com/flex/2/langref/mx/containers/Canvas.html).

Canvas
-->
Container
-->
UIComponent
-->
FlexSprite
-->
Sprite
-->
DisplayObjectContainer
-->
InteractiveObject
-->
DisplayObject
-->
EventDispatcher
-->

All Flex framework components that are rendered to the screen extend from the UIComponent class. All combo boxes and lists implement the IList interface, an AbstractService, DataService or EventDispatcher object implements the IEventDispatcher Interface. You may be using these concepts every day, but weren't aware of it, and how you can use it to your own benefit. Inheritance seems easier to take advantage of at first. Let's say that you want to create several objects, all of which will have identical functions and variables. It is easy to see that you can create a base class that encapsulates all of the common functionality. You can then create sub-classes that implement the differing functionality for each class.

When putting these concepts into real-world Flex applications, you'll need to get familiar with the following keywords:

  • extends (http://livedocs.macromedia.com/flex/2/langref/statements.html#extends): This is used when defining a child class from a parent class.

    public class A extends public class B

  • implements (http://livedocs.macromedia.com/flex/2/langref/statements.html#implements): This is used when implementing an interface.

    public class MyClass implements MyInterface

  • final (http://livedocs.macromedia.com/flex/2/langref/statements. Final methods and properties in OO-languages are generally faster than non-final methods and properties b/c the language does not need to compile declarations that allow the item to be overridden.

    final function myFunction() : void

  • static (http://livedocs.macromedia.com/flex/2/langref/statements.html#static): This is used when creating variables or functions in a class that are specific to the class, not the instance. Static properties and methods do not require variable instantiation to be executed. Static methods and properties in OO-languages are the fastest thing to access becuase there can only be a single instance of that property or method (and thus there is less for the compiler and runtime to process).

    public static function myStaticFunction(): void
    //to use it call it directly from classMyClass.myStaticFunction()

  • internal (http://livedocs.macromedia.com/flex/2/langref/statements.html#internal): This is used when creating a method or property that can be accessed by any object within the same package (namespace).

    internal var foo : String;

  • override (http://livedocs.macromedia.com/flex/2/langref/statements.html#override): This is used when creating a function that overrides a function within a parent class.

    override public function myFunction() : void

  • private (http://livedocs.macromedia.com/flex/2/langref/statements.html#private): This is used when creating methods or properties that are only availaibe to the class where it is defined. A private variable cannot be accessed by outside classes or from descendant classes. Private variables are by nature of OO-languages faster than public vairalbes and shold be used most often.

    private var myPrivateValue : String;

  • protected (http://livedocs.macromedia.com/flex/2/langref/statements.html#protected): This is used when creating methods or properties that are only availaibe to the class where it is defined and descendant classes. A protected variable cannot be accessed by outside classes.

    protected var myProtectedValue : String;

  • public (http://livedocs.macromedia.com/flex/2/langref/statements.html#public): This is used when creating properties and methods that are available to any class.

    public var myPublicValue : String;

  • 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.