aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ambigous method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ambigous method call" Watch "ambigous method call" New topic
Author

ambigous method call

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
hi!
why the compiler is giving error for ambigous method call.shouldn't the integer argumnt method be called.
since it is more specific and every integer can be passed to long as specified in JLS.



madhur.
madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
also
if the method is overloaded in the same class with 2 diff. sinatures
area(int) and
area(long)
then it does not give error
madhur
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi All:
I could not rationalize this error. Is this a bug or some thing?
Thanks
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
I had tried to answer another question about most specific method call yesterday but this one gave a jolt to my basics as i also thought that the method with int should be called. And that was the case when i switched the methods i.e. put the LONG method in rectangle and the INT method in square.
Fortunately i came across an article that tells me that the most specific is not only found by comparing the methods but also by comparing the type to which they belong.
A method is deemed to be applicable if the parameters of the method invocation can be assigned to the arguments of the method invocation without the need for an explicit cast

Going by this rule the method INT in rectangle sounds to be the most specific one. But, when we compare the types we can see that a type of rectangle cannot be assigned to square without explicit type casting. So none of the methods are most specific and hence the "ambigious" error.
For more information Read this
Thanks
Bishal


_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Going by this rule the method INT in rectangle sounds to be the most specific one. But, when we compare the types we can see that a type of rectangle cannot be assigned to square without explicit type casting. So none of the methods are most specific and hence the "ambigious" error.

Hi Bishal:
Did you really mean to say CANNOT in above? I think it should be CAN.
My analysis is that int argument from within method call can be automatically casted to long. So the long method in square is a candidate. But the type of calling object (i.e. square) can be automatically casted to rectangle so int method in rectangle is also candidate. The issue is both of them are candidates by single automatomatic cast. Hence ambiguity: which one to invoke?
If I change long type to byte, then int can not be automatically casted to byte, hence method in square is no more candidate, and I get no error message. At run time, int method in rectangle is invoked.
Thanks. Article you point to was very good.
Barkat
[ September 11, 2002: Message edited by: Barkat Mardhani ]
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
Hi Barkat
I very well meant CANNOT. Maybe i phrased the sentence incorrectly..
Check this out


Now converting this from Java to English i said

But, when we compare the types we can see that a type of rectangle cannot be assigned to square without explicit type casting.

which to me looks ok. Yeah maybe i should have used the phrase "reference of rectangle" instead of "type of rectangle" but then its not so grave an error.
Anyways i am always confused by Java and so it might be that i am still in some confusion. But you can read that article and find out the logic yourselves.
Enjoy!
Bishal
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
your code:
Rectangle r1 = new Rectangle();
Square r2 = new Sqaure();
r2 = (Square) r1;//still error with explicit cast because down-cast
r1 = r2; //ok - because upcast.
That is the point I was trying to make...
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hey Bishal,
That was a good article thx for sharing


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ambigous method call
 
Similar Threads
Loss of precision
Inheritance Doubt
ques from overloading
NoSuchMethodError
Ambiguous Method