• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference to method is Ambiguous

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In reference to this post, I am really confusing myself with ambiguous methods.




Sample #1


Sample #2


Why does code sample#1 compile and run without any errors while code sample#2 causes compilation error ( reference to m is ambiguous)?
What causes two overloaded methods to be ambiguous?

Your help is highly appreciated. Thanks.

EDIT: Sorry for not checking before posting. I modified code sample#1 after Rufat Piriyev's reply.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check your code . Sample #1 doesn't compile
 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rufat Piriyev wrote:
Please check your code . Sample #1 doesn't compile



Sorry for inconvenience. Fixed it now.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18428
hope reading this will help you
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Anoo,

This kind of question is discussed many times here.

The sample 2 won't compile because both of the declarations of m are accessible and applicable and neither one is more specific then the other.
That's why ambiguity is caused in the sample 2.

Please check this for a detailed explanation.

Hope this helps,
 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:
The sample 2 won't compile because both of the declarations of m are accessible and applicable and neither one is more specific then the other.
That's why ambiguity is caused in the sample 2.





I feel promoting 'new C()' to A and matching m is more specific (than promoting it to Q), so 'code 1' is a match. But I realize that is not how it works here. I want to know why? How are promotions done to match arguments and chose a method?

I have gone through link given by kevinn and Prithvi. But I am not able to apply it here. I am missing something. I have also searched this forum and net (though not very exhaustive) , but could not understand better.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the link Prithvi provided:

If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

Note the part in bold. You can apply this logic to this particular problem. Hope this helps.
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic