Class syntax in Actionscript3.0

The package wrapper
With AS3 some changes have been made to the way classes are to be defined. One of the most obvious ones is that class files now have a package wrapper in them.

package com.rubenswieringa {
  public class MyClass {
    public function MyClass ():void {
    }
  }
}

Leaving behind the utter basics, I figured that, being that the class has a package wrapper, perhaps multiple classes could now be defined within one .as file. Wrong. In Flex Builder 2, attempting to declare multiple classes in one package wrapper will result in errors, regardless of their nature (internal, public, etc.).
Fortunately, in the AS3 version for the Flash9 Alpha, packages are allowed to carry multiple classes..

Interesting to note is that with AS3, you can now place statements outside of method definitions as follows:

package com.rubenswieringa {
  public class MyClass {
    trace("oi!");
    public function MyClass ():void {
    }
  }
}

These statements will be executed “when the class definition is first encountered and the associated class object is created” (from LiveDocs).

Helper classes
Outside of the package wrapper, however, multiple class declarations are allowed, called helper classes. Here’s an example:

package com.rubenswieringa {
  public class MyClass {
  }
}
class Helper1 {
}
class Helper2 {
}

Note that helper classes are only available to the main class in the same .as file (in the above example it would be ‘MyClass’). Perhaps surprising is that a subclass is also not allowed to use helper classes belonging to its superclass.

Class attributes
What struck me most about class attributes in AS3 is that, even though a class can be declared public, its opposite (private) is not allowed as a class attribute. The protected attribute isn’t allowed for class declarations either.
All in all, classes can be declared either public or internal, of which the latter is the default, meaning that when you do not specify an attribute, the compiler will assume the class is meant to be internal.

Apart from the public and internal keywords, there are two other attributes allowed for class declarations, being dynamic and final. Here, unlike the two previously discussed attributes, having one applied to a class, does not mean the other is prohibited, so a class can be both dynamic and final at the same time.
Making your class final means it cannot be extended and I think having this in Actionscript is a huge plus, since it’s a rather important aspect of OOP.

Last words..
Tons of thanks go out to Trevor McCauley for teaching me most of the logic in the matters discussed in the above..

14 thoughts on “Class syntax in Actionscript3.0

  1. Hi. Why did they incorporate “package” keyword in AS3? Why not live without it? I find it useless.. AS2 was good enough without it, why the changing of class definition syntax?

  2. Pingback: Flash Professional and Flash Builder: The Perfect Pair « ActionScript Shadows

Comments are closed.