• 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

reference to doh is ambiguous

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Marlene!
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 %
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic