Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Method Overload Q  RSS feed

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


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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"" 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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
Perfectly convincing...Thank you very much !

Regards,
- Jayaprakash
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!