Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics issue: clone() attempting to assign weaker access privileges

Let's have this class structure:

public interface TypeIdentifiable {}

public interface TypeCloneable extends Cloneable {
  public Object clone() throws CloneNotSupportedException;
}

public class Foo implements TypeCloneable, TypeIdentifiable {

   @Override
   public Object clone() throws CloneNotSupportedException {
      // ...
      return null;
   }
}

public abstract class AbstractClass<T extends TypeCloneable & TypeIdentifiable> {

   public void foo(T element) throws Exception {
      TypeCloneable cloned = (TypeCloneable) element.clone();
      System.out.println(cloned);
   }
}

I have this compilation error (although the IDE, Intellij in my case, is unable to show the error while coding)

Error:(4, 37) java: clone() in java.lang.Object cannot implement clone() in foo.TypeCloneable attempting to assign weaker access privileges; was public

I know that the compiler is trying to call clone() method from Object instead of TypeCloneable, but I don't understand why. I also tried it casting to TypeCloneable (I suposed the compiler would know which clone() method call in this case, but the same problem).

   public void foo(T element) throws Exception {
      TypeCloneable typeCloneable = (TypeCloneable) element;
      TypeCloneable cloned = (TypeCloneable) typeCloneable.clone();
   }

I'm a bit confused...Can I do something here to force callling clone() from TypeCloneable?

Thanks for the hellp

like image 570
troig Avatar asked Oct 20 '22 00:10

troig


1 Answers

This works for me, (I am guessing it is a problem with the Type & Type upperbound syntax):

interface TypeIdentifiable {}

interface TypeCloneable extends Cloneable {
  public Object clone() throws CloneNotSupportedException;
}

class Foo implements TypeCloneable, TypeIdentifiable {

   @Override
   public Object clone() throws CloneNotSupportedException {
      // ...
      return null;
   }
}

interface TypeCloneableAndIndetifiable extends TypeCloneable, TypeIdentifiable  {

}
abstract class AbstractClass<T extends TypeCloneableAndIndetifiable> {

   public void foo(T element) throws Exception {
      TypeCloneable cloned = (TypeCloneable) element.clone();
      System.out.println(cloned);
   }
}
like image 196
WillShackleford Avatar answered Oct 31 '22 12:10

WillShackleford