my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overload / Override Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overload / Override" Watch "Overload / Override" New topic
Author

Overload / Override

Paulo Freitas
Greenhorn

Joined: Feb 26, 2003
Posts: 17
Hi everybody...
Try to compile these classes. I've got an error (reference to getParam is ambiguous, both method getParam(int) in Base and method getParam(long) in Sub match).
If the line "sub.getParam(i)" is commented, it works fine... Why does it happen with one method but not with the other?!?!?!
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401

Method getParam(long l) in sub could be called with an int via numeric promotion of int to long. But maybe the compiler can't determine whether to do the numeric promotion or to call the int method in the superclass.
Since the variable is of type sub, I'd like the compiler to go with the method in Sub.
Compiler says
"reference to getParam is ambiguous, both method getParam(int) in Base and method getParam(long) in Sub match"

What's the rule, and why does my compiler say this to me?


comp.lang.javascript FAQ: http://jibbering.com/faq/
Vikram Reddy
Greenhorn

Joined: Jan 06, 2003
Posts: 17
Paulo, Garrett..
Here are couple of questions (#1 and #4) and answers with explainations on this problem.
http://www.developer.com/tech/article.php/987161
This article says
How does the compiler select among overloaded methods?
The exact manner in which the system determines which method to invoke in each particular case is also very involved. Basically, the system determines which of the overloaded methods to execute by matching the types of parameters passed to the method to the types of arguments defined in the formal argument list.


The answer would lie in the "exact manner in which the system determines which method to invoke". Can someone please elaborate this statement?
After a little playing around with the program, I am inclined to think that the compiler tries to match the methods defined in the subclass first and moves upward in the hierarchy.
-- Vikram


SCJP-1.4; SCWCD-EE5<br />---------<br />Sky is not the limit......
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to he Ranch Paulo.

JLS 15.12 explains this one.
If several methods are applicable to a method call the compiler must choose the most specific method. If there is no such one, it issues an error.
For a method to be more specific than other:
a) it must be declared in the same type or a subtype of the other and
b) Each of its arguments must have the same type or to be widdening convertible to the corresponding argument in the other method.

sub.getParam(l); //Should call getParam from Sub

OK. Only Sub.getParam(long) is applicable because the inherited method Base.getParam(int) has an argument incompatible with long if not narrowed-converted.

sub.getParam1(i); //Should call getParam1 from Sub

OK. Though both Base.getParam1(long) and Sub.getParam1(int) apply, the compiler finds that the one declared in Sub is more specific than the other, due to both the following reasons:
a) int is widdening-convertible to long, and
b) the method that is found to have an argument more specific than the other (Sub.getParam1(int) is declared in a type (Sub) that is widdening-convertible to the other (Base).

sub.getParam1(l); //Should call getParam1 from Base

OK. Only Base.getParam1(long) is applicable because a narrowing convertion would be needed for Sub.getParam1(int) to apply for an argument of type long.

sub.getParam(i); //Should call getParam from Base

Not OK. Compiler finds a method call ambiguity.
Sub.getParam(long) is not more specific than Base.getParam(int) because long is not narrowing-convertible to int without a cast. Notice however that Sub.getParam(long) is applicable to the method call sub.getParam(i).
Base.getParam(int) being also applicable is not more specific than Sub.getParam(long) because Base is a supertype of Sub.
[ March 04, 2003: Message edited by: Jose Botella ]
[ March 04, 2003: Message edited by: Jose Botella ]
[ March 04, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
Jose Botella,
Is the rule of the most especific method aplicable only to instance methods??? I ask you this because if you change base.getParam() to be static, it will compile and produce the expected result.
Paulo Freitas
Greenhorn

Joined: Feb 26, 2003
Posts: 17
Now I got it. The big issue is that the method in the Sub class is blocking the method in the Super class making it unaccessible.
Thanks Vikram and Jose.
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
Leandro,
That's right. A static method will not be inherited by your subclass.
My Study Notes


I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
You are write, they are not inherited... Thankx!!!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
static methods are inherited. Just try them.
The most specific rule applies both instance and static methods.
The following example produces an ambigous method call error, uncoment a line and coment the other to see the other error.
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
Oops! You are right, Jose. I made a mistake... I wanted to write: "A static method will be inherited by your subclass." But, I made a typo.
But, you're right. A static method in a superclass can be shadowed by a static method in a subclass.
Thanks again.
My Study Notes
[ March 06, 2003: Message edited by: Karin Paola Illuminate ]
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
if this rule applies to static and non static, why don't we have compiler errors while attenpting to compile Paulo's code (in the begining of the page), just making all methods static???
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Good question !
I do not know the answer.
A rereading of JLS 15.12 did not help me in figuring out why "sub.getParam(i);" (*) did not yield a compiler error when both getParam methods are made static. But, it seems that the compiler chooses the static method to be called based on the types of the arguments of the method invokation, and, it is satisfied as long as the types of the parameters of the applicable method declarations are hierarquically related (**).
More clearly:
In my previous post both methods m and n yield error because the types I1, I2 and I3 are siblings (not a subtype relationship). But being int widdening covertible to long, sub.getParam(i) is ok

(*) the rest of the examples are ok and obeying the most specific rule
(**) I really mean convertible through a JLS 5.3 Method invocation conversion
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Look what I found in the Java Bug Database!
Bug ID: 4723909 class methods do not conform to JLS 15.12.2.2 definition of most specific ...
java:compiler, class methods do not conform to JLS 15.12.2.2 definition of most specific method, State: Closed, fixed, Reported: Jul 31, ...
http://developer.java.sun.com/developer/bugParade/bugs/4723909.html - February 27, 2003 - 36 KB
TUE FEB 11 03:04 P.M. 2003
Look for the fix in 1.4.2.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
If they ask you this question on the exam, you will have to write in the answer...
Well, it depends. That is, it depends on the release version of the SDK you are using.
Tausif Khanooni
Ranch Hand

Joined: Nov 14, 2002
Posts: 107
Hi Moderator!!
readablity of codes are very bad. Please change the fonts of the codes to make it readable.


"Walking on water and building IT Architecture from <br />specification are easy if and only if both are frozen"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overload / Override
 
Similar Threads
Differance in overriding variable and method
Constructor initialization
default constructors and constructors
null as a parameter to method
method overriding doubt