class super1{ void amethod(long l){ System.out.println("long value"); } } class sub extends sub4 { void amethod(int i){ System.out.println("int value"); } public static void main(String a[]) {new sub3().amethod(10); } } Compile error as reference to amethod is ambiguous.Actually shouldnt it print int value since the most specific argument for 10 is selected. Thanks!
avn: Your post does not contain the complete code. Where are classes sub3, sub4 ? Nonetheless, your question seems to be similar to one I posed here . I am still a bit confused on that one. Can somebody explain that again?
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
i am sorry .Here is the correct code: class super1{ void amethod(int i){ System.out.println("int value"); } } class sub extends super1 { void amethod(long i){ System.out.println("long value"); } public static void main(String a[]) {new sub().amethod(10); } }
Error as ambiguous reference to amethod
[This message has been edited by avn (edited August 10, 2000).]
Following works.. Why... class super1{ } class sub1 extends super1 { void amethod(int i) { System.out.println("int value"); } void amethod(long i) { System.out.println("long value"); } public static void main(String a[]) { new sub1().amethod(10); } }
Originally posted by avn: i am sorry .Here is the correct code: class super1{ void amethod(int i){ System.out.println("int value"); } } class sub extends super1 { void amethod(long i){ System.out.println("long value"); } public static void main(String a[]) {new sub().amethod(10); } }
Error as ambiguous reference to amethod
[This message has been edited by avn (edited August 10, 2000).]
This change works: class super1{ public void amethod(long i){ System.out.println("long value"); } } class sub extends super1 { public void amethod(int i){ System.out.println("int value"); } public static void main(String a[]) { new sub().amethod(10); } } as you can see I've changed argument which can be received by overloaded mathods.I suppose this happens because input to overloaded method is not able to change itself to long type.any more suggestions??
Originally posted by Rajesh Gupta: This change works: class super1{ public void amethod(long i){ System.out.println("long value"); } } class sub extends super1 { public void amethod(int i){ System.out.println("int value"); } public static void main(String a[]) { new sub().amethod(10); } }
The above code works coz sub.amethod is choosen to be the Most specific method. When resolving the method to be invoked, the compiler chooses the most specific method among the maximally specific methods. A maximally specific method is one that is both - accessible and applicable. Here both the methods are maximally specific. Coz 10 can be converted to int (by identity conversion) and to long as well (by widening conversion). When two methods are maximally specific, ONE of them is choosen. By the rule specified in JLS section 15.12 However if the methods are reversed, the compiler cannot resolve the MOST specific method. Hence the error in the original program.
Original code is : class super1{ void amethod(int i){ System.out.println("int value"); } } class sub extends super1 { void amethod(long i){ System.out.println("long value"); } public static void main(String a[]) {new sub().amethod(10); } } fails to compile because of the way u r calling amethod. if u call a methos like new sub().amethod(10L); it will compile and work as u want. now the problem with ur version is there are two amethod() which can accept int parameter the one is amethod(int i) and another is amethod(long i) so the compiler gets confused. make any sense ?
thomas
Ranch Hand
Joined: May 26, 2002
Posts: 79
posted
0
Deepak: Irrespective of in which classes the methods are defined, the one with the integer type parameter IS THE MOST SPECIFIC. Don't you agree? Then why can't the compiler resolve it?
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Thomas- you can't ignore the classes the methods are defined in. JLS section 15.12.2.2 specifies the rules for choosing the most specific method, and the class in which a method is defined is part of it. Basically, a method defined in a base class can never be more specific than a method defined in a subclass - even if its argument list is more specific. If you wish to avoid this sort of situation, then the thing to remember is that when writing a subclass, never overload a method using less specific arguments (e.g. long instead of int) unless you also provide an override using the exact same arguments. If you want, the override can just call the original method using super.methodName(): <code><pre> class super1{ void amethod(int i){ System.out.println("int value"); } }
class sub extends super1 { void amethod(int i){ super.amethod(i); } void amethod(long i){ System.out.println("long value"); } public static void main(String a[]) { new sub().amethod(10); } } </pre></code>
thomas
Ranch Hand
Joined: May 26, 2002
Posts: 79
posted
0
Thanks Jim. Your explanation and your suggestion are very appreciated.