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

Preventing Classes from Being Extended and Methods from Being Overridden

To prevent a class from being extended or a method from being overridden, we precede that class or method definition with the final attribute. For example, the following code defines a class, A, that cannot be extended:

final public class A {
}

Because class A is defined with the final attribute, the following attempt to extend A:

public class B extends A {
}

yields this compile-time error:

Base class is final.

Likewise, the following code defines a method, m( ), that cannot be overridden:

public class A {
  final public function m () {
  }
}

Because class m( ) is defined with the final attribute, the following attempt to override m( ):

public class B extends A {
  override public function m () {
  }
}

yields this compile-time error:

Cannot redefine a final method.

The final attribute is used for two reasons in ActionScript:

  • In some situations, final methods execute faster than non-final methods. If you are looking to improve your application's performance in every possible way, try making its methods final. Note, however, that in future Flash runtimes, Adobe expects non-final methods to execute as quickly as final methods.

  • Methods that are final help hide a class's internal details. Making a class or a method final prevents other programmers from extending the class or overriding the method for the purpose of examining the class's internal structure. Such prevention is considered one of the ways to safeguard an application from ...

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