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

Using Generics "&" Operator on an interface defining clone() Cannot Compile

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Having an interface that elevates "clone()" to public visibility, using that interface in a Generics multiple-extension declaration does not compile, eg:

      <T extends CloneInterface & Cloneable>

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to compile the source.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compilation succeeds
      ACTUAL -
      WontCompile.java:8: error: clone() in Object cannot implement clone() in WontCompile
          static <T extends WontCompile & Cloneable> T identity(T t) {
                  ^
        attempting to assign weaker access privileges; was public

      ---------- BEGIN SOURCE ----------
      package compilethis;

      public interface WontCompile {

          // Force it to public
          WontCompile clone();

          static <T extends WontCompile & Cloneable> T identity(T t) {
              return t;
          }

      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: