Here a simple code block:
public class CYoungMan extends YoungMan{
@Override
public String meet(Object obj) {
return obj.getClass().getSimpleName();
}
@Override
public String meet(String str) {
return "String class";
}
public static void main(String[] args) throws Throwable {
Object str = "katie";
YoungMan ym = new CYoungMan();
ym.meet(str);
}
}
For a method call ym.meet(str)
, as we know,
- compiler determines a symbolic reference combined by the class
YoungMan
and the methodString meet(Object obj)
; - then in the runtime, JVM locate the method block by runtime type of
ym
and its inheritance hierarchy.
My question is, why not take (1) to be implemented in runtime and we locate the overload method by runtime type of parameters?
What I more concern is, why Java designers decided to do that (as I described in (1) and (2) ) and why not chose Multiple dispatch in runtime instead (like My suggestion) ? Is there any official explanation?