HardExperience level: 8-15 years3 min read

Bounded Wildcards & PECS — Interview Questions

Reviewed by Gurusankar M.

Asked inGoogleAmazonMicrosoftOracle
#generics#wildcards#pecs#variance#bounded types
Report issue

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.

On this page

Concept Made Simple

Coffee Chat Question

“How do bounded wildcards and the PECS rule let you design flexible generic APIs?”

Remember It Faster

Mind Map Answer

Generics are invariant: List<Integer> is NOT a List<Number>. Wildcards reintroduce controlled variance so APIs can accept related types without losing type safety.

? extends T
Producer — read T out (covariant)
? super T
Consumer — put T in (contravariant)
PECS
Producer Extends, Consumer Super
Recursive bound
<T extends Comparable<T>>

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.

Learn by Doing

Hands-on Keyboard

java
// 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

Think Beyond the Expected

What If?

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:

  • generics are invariant
  • ? extends = producer/read
  • ? super = consumer/write
  • PECS mnemonic
  • recursive bounds for self-comparison

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

  1. Why is List<Integer> not a subtype of List<Number>?
  2. What does <T extends Comparable<? super T>> buy you?
  3. How do wildcards relate to type erasure at runtime?

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.