• 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

method overloading question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain why the compiler finds the method yy ambigous had we commented out the statement below and accepts the call to method xx ?

public class Test {
public static void main(String argv[]){
Test t1 = new Test();
B b1 = new B();
t1.xx(4);
// b1.yy(3);
}
public void xx (int i) {System.out.println("a");}
public void xx (long i) {System.out.println("b");}
}

class A {
public void yy (int i) {
System.out.println(i);
}
}
class B extends A {
public void yy (long i) {
System.out.println(i*2);
}
}

[This message has been edited by Can Candan (edited June 22, 2001).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried both java 1.2 on win NT and 1.2, 1.3, even 1.18 on HPUX, they all compile/run fine without any warning/error:
$ javac test.java
$ java test
a
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Can!
I think calling the method yy()with argument 3L instead of 3 should solve the problem.because you have method yy(long i) in class b,which has long i as a parameter,so in the method call also you should have 3L
hope it helps..
 
Can Candan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
yes by changing
b1.yy(3);
to
b1.yy(3L);
makes the call no longer ambiguous. The question is why we dont need to make that same change when we are calling xx
as t1.xx(4L)
I mean, the compiler does'nt seem to complain when the method is overloaded in the same class, whereas it does complain if it is overloaded in a subclass.
Is there a reason for this different behavior?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to explain with rules in JLS 15.12 - method invocation. At complie time, we need determine method signature. According to JLS 15.12.2.2-choose the most specific method. Here are the rules:

If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
The precise definition is as follows. 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.

So for your question:
case 1:

both method yy(int i) and (long i) are accessible and applicable. But here A can NOT convert to B although int i can convert to long i. Similarly, B can convert to A but long i can NOT convert to int i. Therefore, neither method is SPECIFIC to the other, you got ambiguous complie error.
case 2:
if you change b1.yy(3); to b1.yy(3L), only ONE method is applicable, it is yy(long i).
case 3: interesting one if you change B b1 = new B() to A b1 = new B(), ie.

you get result 3, because at compile time, type A (variable b1) has only ONE method applicable. If you now change the call to b1.yy(3L), you got compile error for method not found.
case 4: if you switch method in A and B, declare bi A;

you got output 3 although int parameter is passed, at compile time, only ONE method is applicable.
case 5: this is the case that one method is more specific than other

you got output 6. Because you have two method again applicable and B can convert to A and int i can convert to long i, B's method is more specific than A's. This is the same as you define two methods in same class in the example.
Overloaded method is choosen at compile time, overriden method at runtime, they are different.
hope this helps. please indicate if I am wrong.

[This message has been edited by Haining Mu (edited June 22, 2001).]
[This message has been edited by Haining Mu (edited June 22, 2001).]
 
Can Candan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for this great explanation
 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic