O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Constructor Methods in Subclasses

Now that we've studied the behavior of instance methods and instance variables in relation to inheritance, let's turn our attention to constructor methods.

Recall that a constructor method initializes the instances of a class by:

  • Calling methods that perform setup tasks

  • Setting variables on the object being created

When a class is extended, the subclass can define a constructor method of its own. A subclass constructor is expected to:

  • Perform setup tasks related to the subclass

  • Set variables defined by the subclass

  • Invoke the superclass constructor method (sometimes called the superconstructor)

A subclass constructor method, if provided, is required to invoke its superclass constructor, via the keyword super. Furthermore, the superclass constructor invocation must occur before any instance variable or instance method is accessed. If no such invocation is provided, the compiler adds a no-argument superclass constructor call automatically. Finally, super must not be used twice in a constructor method.

Forbidding the use of super after any instance variable or instance method is accessed has the following benefits:

  • Prevents methods from being called on an object that has not yet been initialized

  • Prevents variable access on an object that has not yet been initialized

  • Prevents variable assignments in the superclass constructor from overwriting variable assignments in the subclass constructor

Tip

Don't confuse the two forms of the super operator. The first form,

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required