Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading has changed since 1.4.2

 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the code below:

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/15.12.2.3 - 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 ]
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sameer kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What doesn't compile? You said before that the code compiles fine (and even noted what the output was). What code are you trying to compile that doesn't?
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Parnerkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)"
 
charu latha
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using 1.4.2_04:
No compile time error, and outputs "this is the superclass2".
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"

^
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go to this link http://www.sys-con.com/story/?storyid=34292&page=1
and the mystrey is solved !!!
Boy I went NUTS on these one...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic