package SCJP;
class sample
{
sample(String s)
{
System.out.println("String");
}
sample(Object o)
{
System.out.println("Object");
}
}
public class Constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}
In case of sample(Object o) and sample(String o):
String IS-A(n) Object:
so calling a constructor with String arg is more specific that Object; because what is passed to an String can also be passed to object and vice versa is not true; so no ambiguity and call to sample(String args) is made.
In case of sample(String s) and sample(StringBuffer s):
There is no relationship between String and StringBuffer:
null can be substituted to the String and StringBuffer too, so the compiler is confused what to do, which constructor to call. And finally compiler complains that this is ambiguous call;
Regards,
cmbhatt