NOTE: This question is not Enum-related, so it's not duplicate. Enum's are forced to compare only-with-itself because compiler generation of type parameter, not because java recursive type parameter.
I'm trying to find an advantage for declaring a class as:
public class Some<E extends Some<E>>
versus declaring it as:
public class Some<E extends Some>
I have tried providing methods returning E
and methods returning Some<E>
, different cross-calls in complicated class hierarchy and every time I've tried to remove additional <E>
- no new errors/warnings came up.
Can you show me a method that proves the advantage of this additional <E>
?
I assume that there exists one because of JDK declarations:<E extends Comparable<? super E>>
Responses to other questions on SO gives for example:
With the additional construct, you know that any class that extends
Enum is comparable only against itself
But, I can easily break this theory:
public static class Animal<E extends Animal<E>> {
public boolean compare(E other) {...}
}
public class Cat extends Animal<Cat> { }
public class Dog extends Animal<Cat> { } // Note "Cat" !!!
Despite the generic recursion, i can still compare Dog with Cat:
Dog dog = new Dog();
dog.compare(new Cat());
Transalting theory:
you know that any class that extends Animal is comparable only against itself
this is false - I comapred class Dog which extends Animal with Cat, not itself.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…