aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overload Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overload Q" Watch "Method Overload Q" New topic
Author

Method Overload Q

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132


Why gives compiler error saying reference to somemethod is ambiguous. Both somemethod match.

If Byte replaced by Object, works fine - why?

Thank you in advance


"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
it's simple.

someMethod(null) is eligible to call someMethod(String str)& also someMethod(Byte byte).
Since, it is eligible to call both methods, jvm is not able to decide which method it should call.

If Byte replaced by Object, works fine - why?

It is calling someMethod(String str)- why? sorry, no idea.
[ November 23, 2004: Message edited by: sanjeevmehra mehra ]

thanks & regards,<br />Sanjeev.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Since, it is eligible to call both methods, jvm is not able to decide which method it should call.

This is case even with String and Object. So, question remains same.
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
In general it is ambiguous & should not be compiled.

I don't like guesswork, but as all knows Object is parent class (root), so that's making difference. I hope experts like Corey McGlone, Barry Gaunt, Jason Menard, Dirk Schreckmann can give better (actual) reason.
Federico Zuppa
Greenhorn

Joined: Nov 23, 2004
Posts: 7
I also believe that it chooses the most specific. Between Object and String, String is more specific but Byte and String are at the same level of hierarchy.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
When one someMethod() method accepts any Object, while the other someMethod() method requires a String, the "most specific" rule causes Java to choose the more specific method even if both methods could accept the particular argument.

The following code prints "Hello String":


See the the Java Language Specification section 15.12.2.2:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18428

[ November 23, 2004: Message edited by: Mike Gershman ]
[ November 23, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi Nitin,

Your quote
Why gives compiler error saying reference to somemethod is ambiguous. Both somemethod match.


This is happening because the two somemethods' arguments are "Byte" and "String" respectively. Note that these two are peer classes. So the call using null becomes ambigous since both these peer classes are equally preferable and so, ambigous for complier to determine which one to call.

But
If Byte replaced by Object, works fine - why?


Now the case is different. The "String" extends "Object". So they share a Superclass-Subclass relationship. Now the compiler can stick on to the most specific call. i.e. null is more a String object compared to the Object. So there is no compile-time error since these are not peer classes. Thats it!!!

Remember: In such type of questions see the relationship - whether any is-a relation or peer classes.

Nikhil Kanjulli Menon.
SCJP 1.4
Jayaprakash Padiyar
Greenhorn

Joined: Nov 27, 2004
Posts: 3
Hello,
Not sure but I think that the calling method passes "null" as a literal and hence the method which uses string in the argument list will be invoked rather than the method which uses object in its argument list.

Please do correct me if my understanding is wrong.

Cheers.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
"" is the empty String. It is a literal of type String representing a String of zero characters.

null is a literal of type null. It can be assigned to a reference of any type, including String, but you get a NullPointerException if you try to use a reference variable equal to null to access an object or its members. It is not the empty String.

When choosing between same-named methods to invoke, Java will pick a method which can handle fewer cases over a method which can handle all of the first method's cases and some other cases besides. This is the "most specific" rule.

A method that can only handle an argument of type String or null will be chosen over another method that can handle an argument of any type.
Jayaprakash Padiyar
Greenhorn

Joined: Nov 27, 2004
Posts: 3
Mike,
Perfectly convincing...Thank you very much !

Regards,
- Jayaprakash
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overload Q