Originally posted by sean cee:
Hi, I have an interesting Q for you!
public class AQuestion
{
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 agrs[])
{
AQuestion a = new AQuestion();
a.method(null);
}
}
will print "String version" according to the answer
I have no idea about this one..
AND if the (Object o) is replaced with (StringBuffer sb) then compiler error 'cos java doesn't know which method I am refering to.. so.. I guess there is something about Object class..
Can someone tell me why? Thanks.
************************************************************
Hi! Sean if I understand you correctly, your intention is to pass in an object reference to the method "amethod" and see the output as "object version".
Well, it is possible if you actually pass in an object reference.
When you say,
1.StringBuffer buf = new StringBuffer(),
you are creating an object of type StringBuffer and so now you can pass in that reference to amethod() which is expecting an object.
2.StringBuffer buf;
Here an object is not created. You are just declaring a var of type StringBuffer.
So, when you try to pass in that variable the compiler will complain.
Hopefully this answers your question. If I have misunderstood you please disregard this answer.