JDK-4494762 : RFE: Request for Clarification of JLS 15.12.2.2
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2001-08-21
  • Updated: 2006-11-06
  • Resolved: 2006-11-06
Related Reports
Relates :  
Description
Name: rmT116609			Date: 08/21/2001


Request for clarification of JLS 15.12.2.2

http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html


15.12.2.2 of the JLS explains how methods are searched to find the most
specific method and when the compiler warns of ambiguous methods. In
the Java forum someone posted this query:

http://forum.java.sun.com/thread.jsp?forum=31&thread=159240

----

class Homer {
  float doh(float f) {
    System.out.println("doh(float)");
    return 1.0f;
  }

  char doh(char c) {
    System.out.println("doh(string)");
    return 'd';
  }
}

class Bart extends Homer {
  float doh(float f) {
    System.out.println("doh(float)");
    return 1.0f;
 }
}

class Hide {
  public static void main(String[] args) {
    Bart b = new Bart();
    b.doh('x');//compiler error in this line
    b.doh(1);
    b.doh(1.0f);
  }
}

An error was ocurred:reference to doh is ambigous

----
Many people posted replies and it was clear that even after reading the JLS
they were not clear if an error should be generated and why.

After reading the specification a second time I believe I now understand it - though
an example with ambiguity resulting from class inheritance would make it much 
easier to understand.

PLEASE ADD AN APPROPRIATE EXAMPLE TO THE JLS.

NOTE:
It has been reported that Visual Age for Java does not produce an error with
the above code (I have not been able to verify this). If this is the case it is
clear that the specification is also unclear to JVM developers.

ALSO DOES THE JAVA CERTIFICATION SUITE TEST THIS? IS IT POSSIBLE THAT A
DIFFERENCE IN THE IBM PRODUCT HAS SLIPPED THROUGH THE NET?
(Review ID: 129969) 
======================================================================

Comments
EVALUATION By JLS 8.4.8: 1) Bart inherits doh(char) 2) Bart overrides doh(float) (Ignore the return types. They play no part in compile-time method resolution. Not even with covariant returns.) Thus Bart has two methods, doh(char) and doh(float). There is no ambiguity for the call b.doh('x'). (The spec and compiler may have been ambiguous in the past, but are not ambiguous any longer.) By 15.12.2.2, doh(char) and doh(float) are both applicable by subtyping because char is a subtype of float (4.10.1). Then, by 15.12.2.5, doh(char) is more specific than doh(float) because any invocation handled by doh(char) could be passed on to doh(float) without a type error.
06-11-2006