SE450: Overriding: Method Dispatch (aka Method Resolution) [13/35] |
When an overridden method is invoked, we have to decide which implementation to run. There are two ways to do this: based on the static or dynamic type.
Recall
static refers to properties of the text of a program (synonyms are lexical, compile-time and early).
dynamic refers to properties of an execution of a program (synonyms are run-time and late).
declared type = compile-time type.
actual type = run-time type.
Both forms of method resolution proceed up the class hierarchy, using the superclass relation. In pseudo-code:
lookup(C,m) // return the address of code for method m if (C implements m) then return address of C.m else let B = the superclass of C return lookup(B,m)
Procedurally: if m is implemented in C then return the address of C.m else set C to be the superclass of C and repeat.
Static dispatch (early binding): start lookup with the declared type.
Dynamic dispatch (late binding): start lookup with the actual type.