4.3.1 Overriding Methods
Overriding fields is instrumental for creating class hierarchies. Many design patterns utilize it, but here we will explore only the basic functionality. In order to use overrides in a class, it is required that this class has a parent class. Let us consider the following example:
class Base {
public function new() { }
public function myMethod() {
return "Base";
}
}
class Child extends Base {
public override function myMethod() {
return "Child";
}
}
class Main {
static public function main() {
var child:Base = new Child();
trace(child.myMethod()); // Child
}
}
The important components here are:
- the class
Base
which has a methodmyMethod
and a constructor, - the class
Child
whichextends Base
and also has a methodmyMethod
being declared withoverride
, and - the
Main
class whosemain
method creates an instance ofChild
, assigns it to a variablechild
of explicit typeBase
and callsmyMethod()
on it.
The variable child
is explicitly typed as Base
to highlight an important difference: At compile-time the type is known to be Base
, but the runtime still finds the correct method myMethod
on class Child
. This is because field access is resolved dynamically at runtime.
The Child
class can access methods it has overriden by calling super.methodName()
:
class Base {
public function new() { }
public function myMethod() {
return "Base";
}
}
class Child extends Base {
public override function myMethod() {
return "Child";
}
public function callHome() {
return super.myMethod();
}
}
class Main {
static public function main() {
var child = new Child();
trace(child.callHome()); // Base
}
}
The section on Inheritance explains the use of super()
from within a new
constructor.