it compiles and runs with the output: This is the superclass2 so the method aMethod(int) in picked in class Pointx being more specific HOWEVER: IN JLS 2.0/188.8.131.52 - OVERLOADING - specific methods " Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true: * T can be converted to U by method invocation conversion. * Tj can be converted to Uj by method invocation conversion, for all j from 1 to n. " according to these definition the first (*) is not true in our case, since the T is a superclass. In an earlier post dated march 16 2000 from where I took the example, for this code they get compile error, because of the rules above I use the latest compiler fron Sun, so which one is right ??? Edited by Corey McGlone: Added CODE Tags and formatted code for readability. [ April 29, 2004: Message edited by: Corey McGlone ] [ May 01, 2004: Message edited by: Barry Gaunt ]
"Did anyone understand what I have just explained? ... because I did not!"
2 is typed as an int. So, when you invoke the method, the superclass overloaded version will be invoked every time (and yes your code will compile). (Remember, overloading is resolved at compile time.) The point of the specification that you are quoting has to do with instances in which the overloaded function to call cannot be resolved at campile time. An example is f(double) and f(float), given f(2.0) versus f(2.0f). At that point the rules for identity and widening conversions apply. I hope that helps you out. It is a pretty confusing topic since most of us haven't seen this sort of thing since our undergrad discrete mathematics courses.
but daer my system is giving error at compile time error is this Subpoint.java:12: reference to aMethod is ambiguous, both method aMethod(int) in Pointx and method aMethod(float) in Subpoint match s.aMethod(2); plz anyy body explain sameer kuamr
Ok let me explain this again. when I compile the code a get no error (i use the sun jdk 1.4.2_03) however i,ve seen this example on earlier posts and people said that they got error on it because of the ambiguos method call. I agree with that since if we look at JLS 15.2.2 aMethod(int) which is more specific is defined in the superclass. if we swap the methods it satisfies the two conditions listed in jls 15.2.2 My question is Is this a compiler bug or am I misreading the rule mentioned above thanx
I tried to compile the above code in j2sdk1.4.1_05 and J2SDK.v.1.2.1 and its giving compile time error in both "Reference to aMethod is ambiguous. It is defined in void aMethod(float) and void aMethod(int)"
i tried running this code too. the compiler gives me an error C:\j2sdk1.4.1_03\examples>javac SubPointx.java SubPointx.java:10: reference to aMethod is ambiguous, both method aMethod(int) i n Pointx and method aMethod(float) in Subpointx match s.aMethod(2); ^ 1 error C:\j2sdk1.4.1_03\examples> so why would it do so? any explanations?
Hi guys I've got both the 1.3.1 and 1.4.2 compilers.Tried on both and the results are : 1.3.1 ---FAILED to compile , with an error message saying Pointx.java:20: reference to aMethod is ambiguous, both method aMethod(int) in Pointx and method aMethod(float) in Subpoint match s.aMethod(2); 1.4.2---SUCCEEDED , printing "This is the superclass 2"
Great piece of research, Dan. I took a quick look at the release notes but didn't find anything. Hopefully, you will not mind if I change the title of your post to indicate that a change has occured in the semantics of overloading.