Hi Darshan
What Ben wrote is correct! You will understand a most important thing if you change your code as follows(which will ultimately does not compile because of the ambiguity):
public class AQuestion {
public void method(Integer o) {
System.out.println("Integer Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
AQuestion question = new AQuestion();
question.method(null);
}
}
The compile-time error is as follows:
The method method(Integer) is ambiguous for the type AQuestion
This makes you clear that the Integer and String has no relation at all! Both are from different hierarchies so for the compiler its not clear which method to invoke! But when you look at your example, it has two methods with the same name but different arguments method(Object o) and method(String s), for the compiler its clear that the String is a subclass of Object (as every class in
Java is a subclass of Object) and for this reason the compiler invokes the more-specific version than the more generic version. I repeat, in the modified code above, the ambiguity is so clear and hence the compile-time error.
HTH.
Best regards
Kris