Reputation: 7116
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.
Upvotes: 1
Views: 89
Reputation: 12519
testFunc(String s)
gets invoked because the runtime will choose the variant of testFunc
with the most specific argument. testFunc(String s)
is more specific than testFunc(Object o)
because String
is a subtype of Object
.
Peruse section 15.12.2.5 of the JLS for explicit details.
Upvotes: 6