
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Overriding Methods and Properties
|
183
Overridden
instance
property
(defined in
both
superclass
and sub-
class)
class A {
public var overriddenProp:Number = 3;
public function methOfA( ):Void {
trace(overriddenProp);
}
}
class B extends A {
public var overriddenProp:Number = 4;
}
// Usage
var aInstance:A = new A( );
var bInstance:B = new B( );
aInstance.methOfA( ); // Displays: 3
bInstance.methOfA( ); // Displays: 4
Even though the property is
accessed in class A, when
methOfA( ) is invoked on an
instance of B, the subclass’s
value of the overridden prop-
erty (4, not 3) appears
because
bInstance can
store only one value in
overriddenProp. (In Java,
calls to methOfA( ) would
always display 3.)
Class
method of
subclass,
unquali-
fied refer-
ence
a
class A {
public function methOfA( ):Void {
classMethOfB( ); // Error!
}
}
class B extends A {
public static function classMethOfB( ):Void {
}
}
Compile-time error. Class A
does not define classMethOfB(
), and methods of a superclass
can’t resolve unqualified ref-
erences to class methods of a
subclass, so the reference is
illegal.
Class
method of
subclass,
qualified
reference
a
class A {
public function methOfA( ):Void {
B.classMethOfB( ); // Executes properly
}
}
class B extends A {
public static function classMethOfB( ):Void {
}
}
B.classMethOfB( )is a qualified
reference, so the compiler ...