What is PECS (Producer Extends Consumer Super)?
PECS stands for Producer Extends, Consumer Super
. It's the golden rule for using wildcards in Java Generics for maximum flexibility and type safety. The extends
keyword is used when your collection is intended to produce objects. Meanwhile, super
is applied when your collection is "consuming" or accepting elements. Here's a quick look:
Succinctly, the use of extends
is for reading, while super
is for writing to a collection.
Now, let's extend
our knowledge and super
charge your understanding of PECS!
Concrete understanding of PECS
Safe reading with extends
When we're producing or retrieving objects, we use extends
. This guarantees that it's safe to read elements from a collection consisting of T
or any of its subtypes.
Safe writing with super
When we're adding or consuming objects, we lean on super
. This allows us to put an object of type T
or its supertypes safely into a collection.
The in-between: bidirectional collections
For bidirectional collections—we read and write—skip the extends
and super
wildcards. They make a collection either read-only or write-only. Use a plain type parameter to preserve flexibility:
Navigating type substitution
Java abides by the Liskov Substitution Principle (LSP). This rule states that subtypes must be substitutable for their supertype, which is ensured by extends
and super
.
Bounding with wildcards
Bounded wildcards (? extends T
and ? super T
) introduce upper and lower bounds, making your generic methods work with a type that is a subtype or supertype of T
.
PECS in action: Practical scenarios
Reading from covariant collections
When only reading elements, extends
is your keyword. You're safe to retrieve elements from a collection filled with T
or a subtype.
Writing into contravariant collections
When adding elements, super
comes to play. You can add an object of type T
or its supertypes to a collection.
Handling invariance
Lastly, invariance means that only T
can be used, not its subtypes or supertypes.
Smart practices
Constraints of wildcards
Wildcards (?
) represent an unknown type. Collections equipped with them can be either consumers or producers, but not both.
Fetching with surgical precision
Retrieve items from a collection with extends
and you'll always get the right type:
Wide acceptance with super
Add items to a collection with super
, and it's like a warm invitation to a host of types:
Was this article helpful?