User Guide
Classes 117
Constructor methods can only be public, but the use of the public attribute is optional. You
cannot use any of the other access control specifiers, including
private, protected, or
internal, on a constructor. You also cannot use a user-defined namespace with a constructor
method.
A constructor can make an explicit call to the constructor of its direct superclass using the
super() statement. If the superclass constructor is not explicitly called, the compiler
automatically inserts a call before the first statement in the constructor body. You can also call
methods of the superclass using the
super prefix as a reference to the superclass. If you decide
to use both
super() and super in the same constructor body, be sure to call super() first.
Otherwise, the
super reference will not behave as expected. The super() constructor should
also be called before any
throw or return statement.
The following example demonstrates what happens if you attempt to use the
super reference
before calling the
super() constructor. A new class, ExampleEx, extends the Example class.
The ExampleEx constructor attempts to access the status variable defined in its superclass, but
does so before calling
super(). The trace() statement inside the ExampleEx constructor
produces the value
null because the status variable is not available until the super()
constructor executes.
class ExampleEx extends Example
{
public function ExampleEx()
{
trace(super.status);
super();
}
}
var mySample:ExampleEx = new ExampleEx(); // output: null
Although it is legal to use the return statement inside a constructor, it is not permissible to
return a value. In other words,
return statements must not have associated expressions or
values. Accordingly, constructor methods are not allowed to return values, which means that
no return type may be specified.
If you do not define a constructor method in your class, the compiler will automatically create
an empty constructor for you. If your class extends another class class, the compiler will
include a
super() call in the constructor it generates.