SE450: Java: Overriding [6/41] ![]() ![]() ![]() |
static and dynamic: What do these words mean?
Dynamic versus static dispatch.
The word static refers to properties of the text of a program (some synonyms are lexical and compile-time).
The word dynamic refers to properties of an execution of a program (a synonym is run-time).
Dynamic binding proceeds as follows:
Overriding:refers to the introduction of an instance method in a subclass that has the same name, signature, and return type of a method in the superclass. Implementation of the method in the subclass replaces the implementation of the method in the superclass.
All method in Java are polymorphic (virtual in C++) unless they are
declared final in which case they can't be overridden
Overridden methods are chosen at run time (dynamically), overloaded methods are chosen at compile time (statically).
Example:
class B {
public void m2()
...
}
class C extends B {
public void m2()
...
}
B b = new B();
C c = new C();
b.m2(); // invoke the m2( ) in class B
c.m2(); // invoke the m2( ) in class C
Overriding a method with a different signature is not allowed:
class B {
public void m3(int i) {
//
}
}
class C extends B {
public void m3(char c) {
//
}
}