简体   繁体   中英

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.

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 */ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM