What is PECS (Producer Extends Consumer Super)?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
PECS stands for "Producer Extends, Consumer Super." It is a Java generics guideline for deciding whether a wildcard should use extends or super when working with collections.
The rule is short, but the idea behind it is important. If a structure produces values for you to read, use extends. If it consumes values that you want to put into it, use super.
Producer: ? extends T
Use ? extends T when the collection is a source of T values and you mainly read from it:
Why extends? Because List<Integer> and List<Double> should both be acceptable as producers of Number.
The tradeoff is that once you accept List<? extends Number>, you generally should not add new values to it. Java does not know the exact subtype inside the list, only that it extends Number.
Consumer: ? super T
Use ? super T when the collection is a destination for values you want to write:
This works because a List<Integer>, List<Number>, or List<Object> can all consume Integer values safely.
The tradeoff is on the read side. When you get a value back from List<? super Integer>, the safe type is only Object, because Java does not know the precise element type beyond "some supertype of Integer."
Why PECS Matters
PECS is about balancing flexibility and type safety. Without wildcards, methods become too narrow:
This would reject List<Integer> and List<Double>, even though both are perfectly valid sources of numbers.
Using List<? extends Number> makes the method more useful without sacrificing safety.
A Practical Copy Example
The classic example combines both sides:
Here:
- '
srcis a producer, so it usesextends' - '
destis a consumer, so it usessuper'
This is PECS in one method signature.
When You Do Not Need a Wildcard
If a parameter is both heavily read from and written to, a wildcard may not be the right tool. Sometimes a plain type parameter is clearer:
PECS is most useful when the collection has a one-sided role: mainly producer or mainly consumer.
Common Pitfalls
The biggest mistake is memorizing the acronym without understanding the read-write tradeoff. extends is good for reading, but restrictive for writing. super is good for writing, but restrictive for typed reading.
Another common issue is using List<Number> where List<? extends Number> was intended. That makes the API less flexible than necessary.
Developers also sometimes apply wildcards everywhere even when a plain type parameter would be clearer. PECS is a guideline, not a command to add wildcards to every generic signature.
Finally, remember that generics in Java are invariant. List<Integer> is not a subtype of List<Number>, which is exactly why PECS exists.
Summary
- PECS means "Producer Extends, Consumer Super."
- Use
? extends Twhen a collection produces values you read. - Use
? super Twhen a collection consumes values you add. - '
extendsimproves read flexibility, whilesuperimproves write flexibility.' - Use plain type parameters instead of wildcards when the collection needs to be both read from and written to in a balanced way.
Related reading
- What is the best way of implementing a singleton in Python?
- What is the difference between ? and Object in Java generics?
- What is the difference between Builder Design pattern and Factory Design pattern?
- What is the difference between ''E'', ''T'', and ''?'' for Java generics?
- What is pom packaging in maven?
- What is purpose of ConditionalOnProperty annotation?
- What is the difference between Factory and Strategy patterns?
- What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.