Bounded Wildcards
Fast recall
Quick revision
Core answer
extends T> is an upper bound (any subtype of T — read from it), super T> is a lower bound (any supertype of T — write to it). The PECS rule guides you: Producer Extends, Consumer Super — use extends when a structure produces values, super when it consumes them.Whether you understand PECS and why you can't add to a List<? extends T>
Trying to add elements to a <? extends T> list, or confusing which bound allows reads vs writes
Explain the read/write asymmetry: extends gives safe reads, super gives safe writes
Interview-ready guide
Interview answer
A wildcard, written as a question mark, means some unknown type, and a bounded wildcard puts a limit on it.
There are two kinds. Question mark extends T is an upper bound — it means the type is T or some subtype of T. Because the type is at least a T, you can safely read values out as T.
But you can't add anything, except null, because the compiler doesn't know the exact subtype, and adding the wrong thing could corrupt the collection. Question mark super T is a lower bound — the type is T or some supertype of T. That's the opposite situation: you can safely add T values, because a T fits into any supertype container, but when you read, all you're guaranteed is Object.
The way to remember which to use is the PECS rule: Producer Extends, Consumer Super. If a parameter produces values that you're going to read, use extends. If it consumes values that you're going to put in, use super.
The classic example is a copy method: the source list produces elements, so it's a List of question mark extends T, and the destination consumes elements, so it's a List of question mark super T. That lets you copy a List of Integer into a List of Number, which is exactly the flexibility wildcards buy you.
The most common mistake is trying to add to an extends list and being surprised it won't compile, and the second is reversing PECS. Just remember: extends for reading, super for writing.
Bounded Wildcards
Direct Answer
Bounded wildcards restrict an unknown type: <? extends T> is an upper bound (any subtype of T — read from it), <? super T> is a lower bound (any supertype of T — write to it). The PECS rule guides you: Producer Extends, Consumer Super — use extends when a structure produces values, super when it consumes them.
Common Mistakes & Pitfalls
- Adding to
<? extends T>. You can't (exceptnull) — the exact subtype is unknown. - Reading useful types from
<? super T>. Reads only yieldObject. - Reversing PECS. Producer Extends, Consumer Super — mixing them up breaks the API.
- Overusing wildcards. For a method's own type relationships, a named type parameter
<T>is often clearer.
Check your understanding
Answer these without looking back at the article.
- Why can't you add to a List<? extends Number>?
- What is type erasure?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.