Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4067106

Overloading ambiguity detection is inconsistent

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • None
    • 1.2.0
    • tools
    • None
    • generic
    • solaris_2.5.1

    Description

      This code:

      class Class1 {
          void fred(String aString) {
             System.out.println("a");
             System.out.println(aString);
          }
      }
       
      public class Test2 extends Class1
      {
             void fred(Object anObject) {
                     System.out.println("b");
                     System.out.println((String)anObject);
             }
       
             public static void main(String args[])
             {
                     Test2 obj = new Test2();
                     obj.fred("Hello World"); // no trouble doing this
             }
       }

      produces the message:
      Test2.java:18: Reference to fred is ambiguous. It is defined in void fred(java.lang.Object) and void fred(java.lang.String).
                     obj.fred("Hello World"); // no trouble doing this
                             ^

      However, swapping the two "Fred" methods:
      class Class1 {
             void fred(Object anObject) {
                     System.out.println("b");
                     System.out.println((String)anObject);
             }
      }
       
      public class Test2 extends Class1
      {
          void fred(String aString) {
             System.out.println("a");
             System.out.println(aString);
          }
       
             public static void main(String args[])
             {
                     Test2 obj = new Test2();
                     obj.fred("Hello World"); // no trouble doing this
             }
       }

      seems to work fine. My reading of the section of the JLS governing
      overloading resolution is that both should work. In particular,
      15.11.2.2 makes no reference to the origin of the methods when testing
      for maximal specificity.


      Attachments

        Issue Links

          Activity

            People

              tturnidgsunw Todd Turnidge (Inactive)
              dstoutamsunw David Stoutamire (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: