File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reference to doh is ambiguous

 
Siva kandasamy
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
pallavi pandey
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Marlene!
 
Siva kandasamy
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic