• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a question about overload in subclass

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code 1.
class OverloadMethodFather{
void methodA(long l){
System.out.println("long...");
}
}
public class Test extends OverloadMethodFather{
public static void main(String[] args) {
new Test().go();
}
void go(){
methodA(3); //compile err
methodA(3L); //compile err
methodA(3F); //compile OK
}
void methodA(float f){
System.out.println("float...");
}
}

code 2.
public class OverloadMethod_1 extends OverloadMethodFather_1{
public static void main(String[] args) {
new OverloadMethod_1().go();
}
void go(){
methodA(3); //compile OK
methodA(3L); //compile OK
methodA(3F); //compile OK
}
void methodA(long l){
System.out.println("long...");
}
}
class OverloadMethodFather_1{
void methodA(float f){
System.out.println("float...");
}
}
please tell me why caused the error in code 1,however not in code 2.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is explained in JLS 15.12.2.1
In plain English:
Compiler looks for a declaration, that matches the invocation, in the declared type (compile type) of the reference used for the invocation. And in all it supertypes.
It must be both accesible (regarding access modifiers), and aplicable. A declaration is aplicable for an invocation if the number of parameters is the same, and the type of the parameters in the invocation are the same or widenning asignable to those in the declaration.
If several methods are both asignable and accessible the most especific is chosen. If there is not one declaration that is more
more especific to the others declarations, compilers complains saying that the method call is ambigous.
A method declaration is more especific than other if it resides in the same class or a subclass of the one containning the other declaration; and, each type of its parameters is the same or can be asigned to the type of the parameters of the other via a widenning convertion.
In the first example:
methodA(3); //compile err
because an int is both widenning asignable to long or float, the two methodA are aplicable. Given that both are accessible we need to choose the most especific. Well, long is widenning asignable to float, but it is on the father class, so it can't be widenning asigned to its subclass. As a result none of them is more especific and the invocation is ambigous.
methodA(3L); //compile err
the same happens here. long is aplicable to both methods but none of them is more especific.
methodA(3F); //compile OK
only the method receiving a float is aplicable to 3F.
The second code can be reasoned similarly.
Please read this to place the code between marks that will ident it. http://www.javaranch.com/ubb/ubbcode.html
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


so it can't be widenning asigned to its subclass


it should be read :
so the father class can't be widenning converted to its subclass
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at these discussions -
Overloading across classes
Overloaded Methods in Subclass
HTH,
- Manish
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the methods both overloading and the one being overloaded are different mothed. so the jvm actually knows which method it should to execute. so I think this is a question about the execution of overload method which arguments can be widening convert to difference type. like this:
<code><pre>class O1 implements I1,I2//I1 and I2 are interface
...
void amethod(I1 i)
void amethod(I2 i)
...
amethod(new O1())
</pre></code>
occurs compilation error:
C:\dos\myjavas\O1.java:13: error #304: reference to amethod is ambiguous; both method amethod(I1) in class O1 and method amethod(I2) in class O1 match
but the widening conversion is allow in method invocation. what should i do?
reply
    Bookmark Topic Watch Topic
  • New Topic