• Post Reply Bookmark Topic Watch Topic
  • New Topic

overload question  RSS feed

 
robl
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
Prints String Version
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);
}
}
Compile error - please explain
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain this.

-Shallender
 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. your answers are reversed. code 1 gives compiler error. this is because, null may be assigned to both String and Stringbuffer objects. the methods are ambiguous.
2. code 2 gives "string version". although both the methods will accept null arguments, the question here is of the more specific method. i guess you may regard the string class as extending the object class, and therefore providing more functionality. thus, the result
hth,
chetan
 
mehrar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you have the method with the Object type arguement used with method with either StringBuffer or String type argument, it will compile. The idea here is that JVM will execute the method that is lower in the hierachy. But if you create an Object type first and then pass it to the method the JVM will call the method with the Object type arguement.
eg.
public class AQuestion
{
public void method(Object o)
{

System.out.println("Object Version");
}
public void method(StringBuffer sb)
{
System.out.println("String Buffer Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
Object obj=null;
question.method(obj);
}
}
The o/p in this case will be :-
Object Version
 
robl
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank all. Sorry I got the two examples mixed up in the original question.
 
Baskaran Subramani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add one more thing, Both String and StringBuffer classes extends Object and they are at the same level. So if u call a method with a null value, which is overloaded and only takes either String or StringBuffer objects as argument, the compiler gets confused to which method to call!! Because, both are in same level and both can accept null. In case if u have an Object and String as arguments, then it will take String version because it is lower in hierarchy. It holds good for Object and StringBuffer also.

Thanks,
Baskaran.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!