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

Object Orientation
Understanding of OOP (object oriented programming) is fundamental in being successful with the Flex framework and being able to get the most out of it. People without a computer science-related (or similar) background may not know much of the fundamental concepts that comprise OOP and how to apply them correctly, so here is a quick piece to help you out.

First, object-oriented programming is a programming paradigm where your code is organized into logical objects, and each object has properties and methods. Each object contains similar and/or related functionality, and is organized into classes that logically represent and logically organize its functionality.

For example: Let's say that we have a class "Automobile." This class would contain the information and functions necessary for our application to use the automobile class. We could have a numeric property for the number of wheels, the speed, and the direction (degrees on a compass). This class would also contain methods that control the actions of the Automobile object: accelerate, decelerate(break), turn, start engine, stop engine, etc. Our class would look something like this...

public class Automobile
{
    public var speed : Number;
    public var direction : Number;
    public var numWheels : Number;

    public function Automobile()
    { /* constructor */ }

    public function accellerate() : void
    { /* speed up the automobile */ }

    public function decellerate() : void
    { /* slow down the automobile */ }

    public function turn( direction : Number ) : void
    { /* turn the automobile */ }

    public function startEngine() : void
    { /* start the automobile engine */ }

    public function stopEngine() : void
    { /* stop the automobile engine */ }
}

Ok, now that we have a brief explanation of what object-oriented programming is, we can get into some more aspects of OOP: inheritance and interfaces.

Inheritance is a way to form new objects based on existing objects. When a class inherits from a base class, the new class can utilize public and protected properties and methods from the base class. Inheritance can be used to create different objects that utilize functions within the base class, so that the child classes all utilize the same code base. Inheritance can be used to extend the functionality of existing objects, and inheritance can also be used to override and/or change functionality from the base class.

In ActionScript 3.0, you can access the parent class of your class by using the "super" keyword. For instance, calling the constructor of the parent class would use "super()", where accessing a method of the parent class would use something like: "super.myMethodName()". If a property of the parent class is created with public or protected access, you can access that property in the child class directly by the property name (you would use this.propertyName, not super.propertyName).

Now, let's take our automobile example and apply it to inheritance. We already have a base Automobile class that covers the basic functionality. We can create child classes that extend the functionality of the automobile.

public class SportsCar extends Automobile
{
    public function SportsCar()
    {
       super();
    }
    override public function accellerate():void
    {
       /* we can override the accellerate function
         so that it accellerates faster than the base
         Automobile */
    }
}

public class Truck extends Automobile
{
    public function Truck()
    {
       super();
    }




public function tow() : void



{
       /* we can add a tow function that
         allows the Automobile class to tow
         items. */



}
}


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.