This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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!
Joined: Aug 05, 2002
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 ]
Joined: Sep 06, 2002
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
Joined: Aug 05, 2002
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...