Q26.
public class
Test {
public void method(Object o)
{
System.out.println("Object version");
}
public void method(
String s)
{
System.out.println("String version");
}
public static void main(String args[])
{
Test test=new Test();
test.method(null);
}
}
(a)Code does not compile.
(b)Code compiles cleanly & shows "Object version".
(c)Code compiles cleanly & shows "String version".
(d)Code throws a runtime exception.
The answer is (c).The value null can legally be assigned to both the overloaded methods then,how is it determined that the method taking a String argument should be invoked instead of the method that takes an Object argument ???
If the method taking an Object argument is replaced with any other object type(Integer,StringBuffer etc...)then,a compiler error "Reference to method is ambiguous" is displayed .
[This message has been edited by Savio Mascarenhas (edited December 08, 2000).]