3

I'm trying to do something like this, but Java won't let me. I suspect that I am just doing something wrong, but I really have no idea what.

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

It seems to think that T should be a class, not a generic type. My question is similar to this one, but not exactly the same thing. The point of all that is I want to have a type where I can compare it to other things of the same sub-type without being able to compare them to any other PopulationMember object.

Community
  • 1
  • 1
Jeremy
  • 2,826
  • 7
  • 29
  • 25

2 Answers2

4

Try this:

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

}

When you fill in a type parameter (as you do for Comparable), you need to provide a class. If you don't want it to be a specific class, you can parameterize your interface.

danben
  • 80,905
  • 18
  • 123
  • 145
4
interface PopulationMember<T extends PopulationMember> extends Comparable<T>  {
    ...
}

You specify the generic bounds on the current class (interface), not on the extended one.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140