Ambiguous method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laurent Thery

    Ambiguous method

    Given the following example:

    =============== =============== ==============

    class A {
    int method (B a) {
    return 1;
    }
    }
    class B extends A {
    int method (A b) {
    return 2;
    }
    }
    class Test {
    public static void main(String[] args) {
    System.out.prin tln(new B().method(new B()));
    }
    }

    =============== =============== ===============

    When compiling with 1.4.1_02, the compiler is complaining with:

    Test.java:13: reference to method is ambiguous, both method method(B)
    in A and method method(A) in B match
    System.out.prin tln(new B().method(new B()));

    When compiling with 1.4.2, the compiler is happy and the program
    returns 1


    It seems that something has changed with 1.4.2. Unfortunately I could
    not find anything in the changes.
    Anybody can explain me what has exactly changed with method selection

    Thanks

    --
    Laurent Thery
  • Mitch Williams

    #2
    Re: Ambiguous method

    Laurent Thery wrote:
    [color=blue]
    > Given the following example:
    >
    > =============== =============== ==============
    >
    > class A {
    > int method (B a) {
    > return 1;
    > }
    > }
    > class B extends A {
    > int method (A b) {
    > return 2;
    > }
    > }
    > class Test {
    > public static void main(String[] args) {
    > System.out.prin tln(new B().method(new B()));
    > }
    > }
    >
    > =============== =============== ===============
    >
    > When compiling with 1.4.1_02, the compiler is complaining with:
    >
    > Test.java:13: reference to method is ambiguous, both method method(B)
    > in A and method method(A) in B match
    > System.out.prin tln(new B().method(new B()));
    >
    > When compiling with 1.4.2, the compiler is happy and the program
    > returns 1
    >
    >
    > It seems that something has changed with 1.4.2. Unfortunately I could
    > not find anything in the changes.
    > Anybody can explain me what has exactly changed with method selection
    >
    > Thanks
    >
    > --
    > Laurent Thery[/color]

    I'm no expert, but that smacks of a compiler bug to me.

    Comment

    • Doug Pardee

      #3
      Re: Ambiguous method

      thery@sophia.in ria.fr (Laurent Thery) wrote:[color=blue]
      > [snipped example]
      >
      > When compiling with 1.4.1_02, the compiler is complaining with:
      >
      > Test.java:13: reference to method is ambiguous, both method method(B)
      > in A and method method(A) in B match
      > System.out.prin tln(new B().method(new B()));
      >
      > When compiling with 1.4.2, the compiler is happy and the program
      > returns 1
      >
      >
      > It seems that something has changed with 1.4.2. Unfortunately I could
      > not find anything in the changes.
      > Anybody can explain me what has exactly changed with method selection[/color]

      This is due to a change in the Java2 Language Specification. Check out
      http://java.sun.com/docs/books/jls/c...-2-2nd-ed.html with
      an eye on Section 15.12.2.2. The original specification, used by
      1.4.1, said that both the type of the class that the method was
      defined in, and the type of the arguments, were considered when trying
      to determine the "maximally specific" method. The new specification,
      used in 1.4.2, only considers the types of the arguments.

      See also the original problem report at
      Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


      ==========
      Now my occasional reminder to those on this newsgroup:

      comp.lang.java is not an "official" newsgroup. It was replaced by more
      specific newsgroups back in May of 1996. Most people don't see
      postings on comp.lang.java, because only a few news servers still
      carry it. Google Groups carries it for its historical value, and a few
      ISPs (mainly cable-Internet providers) seem to carry it out of
      ignorance :-)

      The current worldwide Java newsgroups are:

      comp.lang.java. 3d 3D Graphics API's for the Java language
      comp.lang.java. advocacy Support for and criticism of the Java System
      comp.lang.java. announce Announcements re the Java System (Moderated)
      comp.lang.java. beans Java software components (JavaBeans)
      comp.lang.java. corba Topics relating to Java and CORBA
      comp.lang.java. databases Databases, java.sql, JDBC, ODBC
      comp.lang.java. gui GUI toolkits and windowing: AWT, IFC etc
      comp.lang.java. help Set-up problems, catch-all first aid
      comp.lang.java. machine JVM, native methods, hardware
      comp.lang.java. programmer Programming in the Java language
      comp.lang.java. security Security issues raised by Java

      Comment

      Working...