
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Overriding Methods and Properties
|
167
methods and properties. We’ll now turn to redefinition, in which a subclass provides
an alternative version of a method or property in its superclass. (Bear in mind that
reuse, extension, and redefinition are not mutually exclusive. A subclass might
employ all three techniques in regard to the superclass’s members.) 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 or prop-
erty is known technically as overriding that method or property.
ActionScript allows any of a class’s members (that is, static properties, instance prop-
erties, static methods, and instance methods) to be redefined. We’ll take a look at
the most typical kind of redefinition first: overriding instance methods.
Overriding Instance Methods
To override a superclass’s instance method, we simply supply an instance method
definition of the same name in the subclass. For example, in the following code, in
which B is a subclass of A, the method B.x( ) overrides the method A.x( ):
class A {
// Declare an instance method in the superclass
public function x ( ):Void {
trace("A.x( ) was called.");
}
}
// Class B is a subclass of class A
class B extends A {
// Override ...