Bounded Wildcards & PECS — Interview Questions
⚡ Short Answer
Wildcards add flexibility to invariant generics. PECS — Producer Extends, Consumer Super — is the rule: use <? extends T> when a structure produces T values you read out, and <? super T> when it consumes T values you put in. This lets a method like copy(dest, src) accept a wider range of type arguments safely. Generic methods and recursive bounds (<T extends Comparable<T>>) round out flexible, type-safe API design.
☕Coffee Chat Question
Concept Made Simple
“How do bounded wildcards and the PECS rule let you design flexible generic APIs?”
🧠Mind Map Answer
Remember It Faster
Generics are invariant: List<Integer> is NOT a List<Number>. Wildcards reintroduce controlled variance so APIs can accept related types without losing type safety.
You can read from a ? extends T (everything is at least a T) but can't add to it; you can add a T to a ? super T but can only read Object out. That asymmetry IS the safety — and PECS tells you which to pick.
Key takeaway: if a parameter is a source you read, use extends; if it's a sink you write to, use super; if you do both, use an exact type T.
⌨️Hands-on Keyboard
Learn by Doing
// PECS in action: src PRODUCES (extends), dest CONSUMES (super)
static <T> void copy(List<? super T> dest, List<? extends T> src) {
for (T item : src) dest.add(item);
}
List<Integer> ints = List.of(1, 2, 3);
List<Number> nums = new ArrayList<>();
copy(nums, ints); // Number super Integer, Integer extends Number — OK🔥What If?
Think Beyond the Expected
Why can't you add an element to a List<? extends Number>?
Because the compiler only knows the list is 'some unknown subtype of Number' — it could be a List<Integer>, a List<Double>, etc. Adding a Double to what might actually be a List<Integer> would corrupt type safety, so the compiler forbids all adds (except null). You can safely READ (every element is at least a Number), which is exactly why <? extends> is for producers you read from. To add elements, you need <? super Number>, where any Number is a valid input.
😂Real World
The JDK is full of PECS: Collections.copy(List<? super T>, List<? extends T>), Stream.forEach(Consumer<? super T>), Comparator<? super T> in sort. Designing library methods with the right wildcards is what lets callers pass Integer lists where Number is expected without casts — a hallmark of well-designed generic APIs in code reviews.
🎯Interviewer's Expectation
Keywords they're listening for:
⚠️Common Mistakes
- ✗Trying to add to a <? extends T> collection
- ✗Using an exact type where a wildcard would generalise the API
- ✗Confusing which side needs extends vs super
✅Best Practices
- ✓Apply PECS to method parameters for flexible APIs
- ✓Use ? super for consumers (e.g. Comparator, Consumer)
- ✓Keep return types concrete; put wildcards on inputs
🔁Follow-up Questions
- 1Why is List<Integer> not a subtype of List<Number>?
- 2What does <T extends Comparable<? super T>> buy you?
- 3How do wildcards relate to type erasure at runtime?
🧩Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Generics (Advanced Java) Interview question: "How do bounded wildcards and the PECS rule let you design flexible generic APIs?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.