Generics - Generics, Bounds, and Wildcards In previous...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Generics, Bounds, and Wildcards In previous assignments in which we have used generic arguments, the object defined by the generic type was usually data wrapped in some sort of node. But in the case of PriorityQueue, the generic argument will be the “node”. Therefore, we cannot settle for any type to serve as the generic type, we must use a type that satisfies the requirements for an entry in a heap. Specifically, we must be able to compare this entry with others, so the entry must implement Comparable. Class Declaration To force the Java compiler to only accept generic arguments that implement Comparable, we must specify bounds on what type is acceptable. To require that the type implements Comparable, we bound E above using the extends keyword in the angle brackets (< >) after the class declaration. That is, saying “E extends T” is equivalent to saying “E is T, E is a subclass of T, or E implements T”. This gives us the following: public class PriorityQueue<E extends Comparable>
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

Generics - Generics, Bounds, and Wildcards In previous...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online