Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null arg in overloaded method

 
Nidhi Juneja
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone here is another code whose reult is unclear
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
O/P String Version
While this code:
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
O/P-reference to method is ambiguous, both method method(java.lang.StringBuffer) in AQuestion and method method(java.lang.String) in AQuestion match
question.method(null);

How r these 2 situations different? I am missing some point?
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the 1st case, the String method is called because it is more specific than the Object method.
In the original code, neither the String method nor the StringBuffer method was more specific, so the compiler complained.
 
Nidhi Juneja
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sunita...now i have understood the concept...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic