When you extend
a class, you can override
methods that the inherited class defines using the override
keyword:
public class Example {
public function test():void {
trace('It works!');
}
}
public class AnotherExample extends Example {
public override function test():void {
trace('It still works!');
}
}
Example:
var example:Example = new Example();
var another:AnotherExample = new AnotherExample();
example.test(); // Output: It works!
another.test(); // Output: It still works!
You can use the super
keyword to reference the original method from the class being inherited. For example, we could change the body of AnotherExample.test()
to:
public override function test():void {
super.test();
trace('Extra content.');
}
Resulting in:
another.test(); // Output: It works!
// Extra content.
Overriding class constructors is a little bit different. The override
keyword is omitted and accessing the inherited constructor is done simply with super()
:
public class AnotherClass extends Example {
public function AnotherClass() {
super(); // Call the constructor in the inherited class.
}
}
You can also override get
and set
methods.