| Author |
Reference to method is Ambiguous
|
Anup Om
Ranch Hand
Joined: Dec 30, 2009
Posts: 62
|
|
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.
|
SCJP6
|
 |
Rufat Piriyev
Ranch Hand
Joined: Mar 15, 2010
Posts: 31
|
|
|
Please check your code . Sample #1 doesn't compile
|
 |
Anup Om
Ranch Hand
Joined: Dec 30, 2009
Posts: 62
|
|
Rufat Piriyev wrote:
Please check your code . Sample #1 doesn't compile
Sorry for inconvenience. Fixed it now.
|
 |
kevinn lee
Ranch Hand
Joined: Feb 15, 2010
Posts: 87
|
|
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18428
hope reading this will help you
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
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,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
Anup Om
Ranch Hand
Joined: Dec 30, 2009
Posts: 62
|
|
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.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
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.
|
[How To Ask Questions][Read before you PM me]
|
 |
 |
|
|
subject: Reference to method is Ambiguous
|
|
|