This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

ambigous method call

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All:
I could not rationalize this error. Is this a bug or some thing?
Thanks
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bishal,
That was a good article thx for sharing
 
eat bricks! HA! And here's another one! And a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic