public void testFunc(Object o) {
System.out.println("testFunc-Object");
}
public void testFunc(String s) {
System.out.println("testFunc-String");
}
Both of these methods are in test class. If I invoke the following method from main method of test class, which method will be invoked?
Test t = new Test();
t.testFunc(null);
In this particular scenario, testFunc(String) is called, but why?
I would appreciate your help.
testFunc(String s)gets invoked because the runtime will choose the variant oftestFuncwith the most specific argument.testFunc(String s)is more specific thantestFunc(Object o)becauseStringis a subtype ofObject.Peruse section 15.12.2.5 of the JLS for explicit details.