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

calling ambiguous method compiles

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b32
    • CPU:
      generic
    • OS:
      generic

      Description

      Calling check("") on a Test3 should be ambiguous, as there are two
      methods with that name and signature, but neither is more specific.

      Code snippet:-
      class Test<T,E> {
       
          
          public void check(T val){
              System.out.println("Second check method being called");
          }
       
          public E check(E val){
              System.out.println("First check method being called");
              return null;
          }
                  
       }
       
       class Test2 extends Test<String,Integer> {
       
       
       }
       
       class Test3 extends Test<String,String> {
       
       
       }
       
       
       public class ParametericMethodsTest3 {
            public void assertion1() {
                  Test2 tRef = new Test2();
                  tRef.check("");
                  tRef.check(new Integer(20));
            }
       
            public void assertion2() {
                  Test3 tRef = new Test3();
                  // gives compilation error, found void required string
                  //String str = tRef.check("");
                  tRef.check("");
                  
            }
           
            public static void main(String args[]) {
                   ParametericMethodsTest3 pRef = new ParametericMethodsTest3();
                   pRef.assertion1();
                   pRef.assertion2();
            }
       
       
       }

        Attachments

          Activity

            People

            Assignee:
            gafter Neal Gafter
            Reporter:
            jsinghsunw Jit Singh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: