Why can't I do this using interfaces?

I am trying to do something similar, but Java will not allow me. I suspect I'm just doing something wrong, but I really don't know what.

public interface PopulationMember extends Comparable<T extends PopulationMember>  {
    int compareTo(T o);
    T merge(T o);
    // Some other stuff
}

It seems that T should be a class, not a generic type. My question is similar to this , but not exactly the same. The bottom line is that I want to have a type where I can compare it with other things of the same subtype, without being able to compare them with any other PopulationMember object.

+3
source share
2 answers

Try the following:

public interface PopulationMember<T extends PopulationMember> extends Comparable<T> {
    int compareTo(T o);
    T merge(T o);
    // Some other stuff

}

( Comparable), . , , .

+4
interface PopulationMember<T extends PopulationMember> extends Comparable<T>  {
    ...
}

(), .

+4

Source: https://habr.com/ru/post/1733217/


All Articles