wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes reference to  doh is ambiguous Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reference to  doh is ambiguous" Watch "reference to  doh is ambiguous" New topic
Author

reference to doh is ambiguous

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi there,
Compile the code below. I get the error "Hide.java:41: reference to doh is ambiguous". I don't follow, why ?. Can you help.

thanks
siva
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Interesting. Jikes says there's an ambiguity, while JDK 1.4.2's javac doesn't. I also checked JDK 1.2.2's javac, and it complains too! If I change "Hide" to use Homer instead of Bart, no compiler complains.
I looked at the Java Language spec to see if there was any information that might be relevant to this, but there is none. What compiler are you using?
I'd consider this a bug in any compiler that complains. Does anybody else have an opinion here?


[Jess in Action][AskingGoodQuestions]
pallavi pandey
Greenhorn

Joined: Nov 06, 2003
Posts: 12
Trying all possible ways.
And this compiles fine.

class Homer {
int doh(int i) {
System.out.println("doh(int)");
return 100;
}
char doh(char c) {
System.out.println("doh(char)");
return 'd';
}

}
class Milhouse {}

class Bart extends Homer {
char doh(char c) {
System.out.println("doh(char)");
return 'd';
}
}
class Hide {
public static void main(String[] args) {
Bart b = new Bart();
//Homer b = new Homer();
b.doh(1);
b.doh('x');
}
}
Anybody to clarify?
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
There is an amendment to JLS 15.12.2.2
http://java.sun.com/docs/books/jls/clarifications-2-2nd-ed.html
The SDK compiler changed between 1.4.1 and 1.4.2 to be consistent with the amendment.
Here is how we explain the ambiguous reference in its simplest form.

B declares m(int) and inherits m(char). The method invocation m(�x�) could use either method declaration.
1. B.m(int) is more specific than A.m(char) if int can be converted (without casting, widened) to char (No) and B is a subclass of A (Yes)
2. A.m(char) is more specific than B.m(int) if char can be converted (without casting, widened) to int (Yes) and A is a subclass of B (No)
Neither method is most specific, hence an ambiguity.
In the amendment to the JLS, the requirement to check if one class is a subclass of another has been removed. Now, one method is more specific than another only if the parameter types of one can be converted to the parameter types of the other.
Since char can be converted to int, m(char) is more specific than m(int) and the compiler chooses the method declaration m(char). No ambiguity.
[ November 07, 2003: Message edited by: Marlene Miller ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Thanks, Marlene!
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi Thanks to All of you.
I noticed problem with version 1.4.1., the error message is placed below.
However, I could able to compile with no issues 1.4.2
The information, you all provided is very useful.
thanks
siva
x38020

~/reading/java/reuse % javac Hide.java
Hide.java:41: reference to doh is ambiguous, both method doh(char) in Homer and method doh(int) in Bart match
b.doh('x');
^
1 error
~/reading/java/reuse % java -version
java version "1.4.1_01a"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01a-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01a-b01, mixed mode)
~/reading/java/reuse %
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reference to doh is ambiguous
 
Similar Threads
Name Hiding
Recursion Help
Whats wrong with this code? Simple problem
Overloaded Methods in Subclass
attempting to use incompatible return type