wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading has changed since 1.4.2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading has changed since 1.4.2" Watch "Overloading has changed since 1.4.2" New topic
Author

Overloading has changed since 1.4.2

Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
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 ]

"Did anyone understand what I have just explained? ... because I did not!"
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
sameer kumar
Greenhorn

Joined: Mar 22, 2004
Posts: 13
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

Joined: Dec 20, 2001
Posts: 3271
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?


SCJP Tipline, etc.
Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
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

Joined: Apr 24, 2004
Posts: 10
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

Joined: Aug 20, 2002
Posts: 67
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

Joined: Aug 03, 2002
Posts: 7729
Using 1.4.2_04:
No compile time error, and outputs "this is the superclass2".


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
jeff mutonho
Ranch Hand

Joined: Apr 30, 2003
Posts: 271
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

Joined: Jan 21, 2004
Posts: 92
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading has changed since 1.4.2