• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ambigous method call

 
madhur jain
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All:
I could not rationalize this error. Is this a bug or some thing?
Thanks
 
Bishal P
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bishal,
That was a good article thx for sharing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic