• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Overloading  RSS feed

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

In the following code , I am getting Output as "Object version".
But, I am unable to understand... why is it not "String version" as null can be assigned to both Object as well as String. How do we decide, which method to invoke in the code.

public class AQuestion
{
public void method(object o){
{
System.out.println("Object version");
}
public void method(String o){
{
System.out.println("String version");
}
public static void main(String args[]){
AQuestion q1 = new AQuestion();
q1.method(null);
}
}

Please guide
-Sethi
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it is true that null can be assigned to both Strings and Objects, think about what a String is...it is an Object as well (it inherits from Object), since it has methods (like length() and charAt()). If null were something that ONLY applied to Strings, the String Version method would probably be called. However, since null is a keyword that can be applied to any Object, Java can't assume you mean "a null String."
[ January 15, 2006: Message edited by: Tom McC ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this program were correct (and there are three errors in it, as I'll detail below) it would print "String version." When the compiler must choose among multiple overloaded methods which all apply to the given arguments, it will choose, if possible the "most specific" one. In this case, because String is a subclass of Object, the String one is more specific, and so that is the one that it called.

Now as to those errors: both "method" methods have extra open braces in them, and the word "object" should be "Object". My guess is that the program you tested is not quite what you think you tested -- it's certainly not the same as what you've shown here, as this won't even compile. Carefully fix this, make sure it compiles, then try again.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom McC:
However, since null is a keyword that can be applied to any Object, Java can't assume you mean "a null String."]


An interesting, but false, explanation. The "most specific" rule is the one that applies.

Tom, has anyone pointed you to our policy on display names yet? Yours is obviously fictitious, which is against the rule. Please go here and fix yours as soon as possible. Thanks.
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I am sorry abt those extra braces...dat was my writing mistake. I have correcting the code..
I have tested the code. It is giving me "Object Version". but i am still unable to understand logic behind this.

public class AQuestion
{
public void method(object o)
{
System.out.println("Object version");
}
public void method(String o)
{
System.out.println("String version");
}
public static void main(String args[]){
AQuestion q1 = new AQuestion();
q1.method(null);
}
}

-thanks
S sethi
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you've still got "object" instead of "Object", so my suspicion is that we're still not seeing the real code. But as I said, this program should not print "object version", and if it truly is, then either you've got a broken Java compiler (not unheard of, I suppose, what with more open-source Java development tools available all the time) or you've done something odd like define your own class named "object" or "String".
 
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also check out what happens when you add another overloaded method: eg one that takes an Integer. It then requires you cast your null to a specific type as it can no longer has a rule for resolving it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!