Jeremy Robertson
Jeremy Robertson

Reputation: 115

Extending comparables with java generics and interfaces

So I'm creating an implementation of a priority queue using generics. I have this interface which I am trying to implement in my PriorityQueue class:

public interface PriorityQueueInterface<Item extends Comparable<Item>> { }

but I'm not sure what the proper syntax is to correctly implement the PriorityQueueInterface. Here is what I currently have:

public class PriorityQueue<Item extends Comparable<Item>> implements PriorityQueueInterface<Item extends Comparable<Item>>{ }

but it's throwing multiple errors. What would be the correct way to implement the interface? Any help would be appreciated.

Upvotes: 0

Views: 53

Answers (1)

rgettman
rgettman

Reputation: 178253

You've already declared Item to be Comparable<Item> with the class definition of PriorityQueue. You only need to reference it in the implements clause, where you don't need to repeat that it's Comparable<Item>. You reference a generic type parameter in the implements or extends clause just as you would for any other part of the class body where the generic type parameter is in scope.

Try

public class PriorityQueue<Item extends Comparable<Item>>
    implements PriorityQueueInterface<Item>{ /* implement here */ }

Upvotes: 1

Related Questions