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

Overriding Instance Methods

In our study of inheritance so far, we've covered reuse, in which a subclass uses its superclass's methods and variables, and we've covered extension, in which a subclass adds its own methods and variables. We'll now turn to redefinition, in which a subclass provides an alternative version of a method defined by its superclass. (Bear in mind that reuse, extension, and redefinition are not mutually exclusive. A subclass might employ all three techniques.)

Redefinition lets us customize an existing class for a specific purpose by augmenting, constraining, or even nullifying one or more of its original behaviors. Redefining a method is known technically as overriding that method.

Tip

ActionScript 3.0 allows instance methods to be redefined but not instance variables, static variables, or static methods.

To override a superclass's instance method, we supply an instance method definition of the same name in the subclass, and precede that definition with the keyword override. For example, consider the following code, which creates a class, A, with an instance method, m( ):

public class A {
  // Declare an instance method in the superclass
  public function m () {
    trace("A's m() was called");
  }
}

Also consider the following code, which creates a class, B, that inherits from A:

// Class B is a subclass of class A
public class B extends A {
}

To override m( ) in B, we use the following code:

public class B extends A { // Override the superclass's method m() override public ...

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