views:

93

answers:

2

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.

+4  A: 

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
That worked perfectly; Thanks!
Jeremy
+4  A: 
interface PopulationMember<T extends PopulationMember> extends Comparable<T>  {
    ...
}

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

Bozho