• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

overload question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain this.

-Shallender
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank all. Sorry I got the two examples mixed up in the original question.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic