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

Allow a class to subtype multiple parameterizations of same generic interface

XMLWordPrintable

    • Verified

      J2SE Version (please include all output from java -version flag):
         Java version "1.8.0-ea"
         Java(TM) SE Runtime Environment (build 1.8.0-ea-b120)
         Java HotSpot(TM) 64-Bit Server VM (build 25.0-b62, mixed mode)


      Does this problem occur on J2SE 6ux or 7ux? Yes / No (pick one)
        Yes


      Operating System Configuration Information (be specific):
         Microsoft Windows [Version 6.1.7601]


      Hardware Configuration Information (be specific):
      All


      Bug Description:
      Currently the following code is causing the compiler to report:
      Copyable cannot be inherited with different arguments: <SubClass> and <BaseClass>
      public class SubClass extends BaseClass implements Copyable<SubClass> {
      1 error
      -----------------------------------------------------------------------------------------------------------------------------------------
      public interface Copyable<T> {
        T copy();
      }

      public class BaseClass implements Copyable<BaseClass> {
        @Override
        public BaseClass copy() {
          return new BaseClass();
        }
      }

      public class SubClass extends BaseClass implements Copyable<SubClass> {
        @Override
        public SubClass copy() {
          return new SubClass();
        }
      }
      -----------------------------------------------------------------------------------------------------------------------------------------

      In early versions of JDK1.5 this would compile and run without a problem, but the compiler was changed to disallow this - in which case this is actually a Regression.

      This is a simple copy() pattern, and it is very hard to do in Java without the compiler allowing this, hence one example why need this to be able to compile.

      Even with the way that Java does Erasure, this is not a problem as although the copy methods will have the same Erasures, the Sub one will Override the Base one, and they will both implement the one from the interface.

      Also this will never present a problem with the Java Class File Format as a Sub Class and Base Class can implement the same interface.

      Likewise all interfaces without any methods using Generic Type Parameters should compile just fine, ie void foo(int aValue), however obviously ones containing Generic Type Parameters such as void foo(T aValue) would be a problem with the way that Java does Erasure as that would cause 2 methods with the same Erasures without one Overriding the other.

      Also believe that the compiler already checks for this so there is not too much that needs to be changed in order to allow for the above code to compile. Also when allowing this some care should be taken to either make a class that implements both Copyable<BaseClass> and Copyable<SubClass> after Erasure to just implement Copyable (and not implements Copyable, Copyable which the Class File Format does not allow), or alternatively only allow this to be done as in my code example.

      Steps to Reproduce (be specific):
        Compile the Code above.

      Expected: No Compile Error
      Actual: There is a Compile Error

            abuckley Alex Buckley
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: