I think you have a more general design problem here. Namely, why do you want to implement compareTo in the base class? In practice, this almost always leads to problems down the line.
Consider
public class Subclass2 extends Superclass<Subclass2> { ... } Subclass sub = new Subclass(); Subclass2 sub2 = new Subclass2();
If you have two instances of Superclass (whose parts of the superclass are identical, but the parts of the subclass cannot be), how can you compare them using Superclass.compareTo ? If you do not account for specific subclasses, the result of the comparison is 0, incorrect. If you try to directly compare objects of different subclasses, you lose with an exception from the runtime. If you try to be smart and check the runtime types of objects before trying to directly compare them, again you need to throw some sort of exception at runtime, rather than returning the correct result.
I think it would be better to cancel the scheme and implement compareTo only in specific subclasses where the compiler can guarantee that the method is called with the correct parameter type. If you are worried about code duplication between subclasses, you can translate it into the protected final method in the superclass.
Update
Code example:
public abstract class Superclass { protected final int compareBasePortionTo(Superclass other) { ... }; } public class Subclass extends Superclass implements Comparable<Subclass> { public int compareTo(Subclass other) { int baseCmp = compareBasePortionTo(other); if (baseCmp != 0) return baseCmp;
source share