Search code examples
javagenerics

The inherited method Object.clone() cannot hide the public abstract method


So, I have this bit of wild, crazy code, that is making the compiler spit in my face for some hours the following error:

The inherited method Object.clone() cannot hide the public abstract method in IOrderable<T>

The culprit classes are the following (the error appears right in the T of the generic):

public class MyInterval<T extends Xpto & Successorable<T>> implements Cloneable {
    public MyInterval<T> clone(){
        MyInterval<T> it = null;
        try {
            it = (MyInterval<T>) super.clone();
            it.max = it.max.clone();
            it.min = (T) it.min.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return it;
    }
}

public interface Xpto {}

public interface Successorable<Y> extends IOrderable<Y> {
    Y suc();    
}   

interface IOrderable<J> extends Rankable<J>, Cloneable {
    boolean greaterEq(J e);
    J clone();
}

public interface Rankable<P> {
    int rank(P e);
}

Yes, they seem kinda random. They solely exist to test some weird things in a compiler/java_byte_code_instrumentation-like project I am doing. How can I make this work while keeping the logic intact?


Solution

  • I encountered the same compilation error, and as far as I can tell, this is an edge case in the specification for intersection types (multiply-bound generic types), and there is no solution possible.

    A small test case:

    public interface InterfaceA {
        public Object clone();
    }
    
    public interface InterfaceB {}
    
    public class ClassA<T extends InterfaceA> {} // ok
    
    public class ClassB<T extends InterfaceA & InterfaceB> {} //not ok
    

    You can't have an intersection type containing any interfaces containing any method whose signature matches a non-public method on the base class (in this case, implicitly Object). This is not specific to Object or clone, however:

    public abstract class Base {
        protected abstract void m();
    }
    
    public interface Interface {
        public void m();
    }
    
    public class Class<T extends Base & Interface> {} // not ok
    

    Here's the relevant Oracle bug, marked as "Not a defect".

    Here's the relevant Eclipse bug, where the compiler was modified to produce the same error to match javac. This appears to have been a change for Indigo 3.7, which explains why some people could reproduce and others couldn't.