Search code examples
javagenericscomparablebounded-wildcard

Java: Having trouble implementing bounded generics interface


I searched through a lot of questions and other internet articles, but I can't seem to find the one that caters to my specific case, and none of the other ones solutions worked for me.

I have this interface here:

public interface PriorityQueueInterface<T extends Comparable<? super T>>

I need to make a priority queue class to implement this interface, so I typed it out like this:

public class ArrayPriorityQueue<T> implements PriorityQueueInterface<Comparable<? super T>>

However, it is not compiling as I get this error:

type argument Comparable is not within bounds of type-variable T#2 where T#1,T#2 are type-variables: T#1 extends Object declared in class ArrayPriorityQueue T#2 extends Comparable declared in interface PriorityQueueInterface

I tried all types of combinations, but nothing seems to work. How do I write the class declaration so that it compiles?


Solution

  • Seems like what you want is to declare the type variable with the same bound, then pass that on as an argument to the interface:

    public class ArrayPriorityQueue<T extends Comparable<? super T>>
        implements PriorityQueueInterface<T> {...}