Trying to create a superclass that guarantees that all subclasses are inherently Comparable.
static class Distinct<T extends Comparable<T>> implements Comparable<Distinct<T>> {
final T it;
public Distinct(T it) {
this.it = it;
}
@Override
public int compareTo(Distinct<T> o) {
return it.compareTo(o.it);
}
}
static class ThingHolder<T extends Comparable<T>> {
final Set<T> things;
public ThingHolder() {
this.things = new TreeSet<>();
}
}
static class Thing extends Distinct<String> {
public Thing(String it) {
super(it);
}
}
final ThingHolder<Thing> yz = new ThingHolder<>();
The error I am reading is:
com/oldcurmudgeon/test/Test.java:[70,22] error: type argument Thing is not within bounds of type-variable T
where T is a type-variable:
T extends Comparable<T> declared in class ThingHolder
Why is this not working? It can be done?
source
share