Wildcards and PECS
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write flexible generic method signatures.
Planned topics
List<Integer>is not aList<Number>: generics are invariant.? extends T: a producer; you can readT, not add.? super T: a consumer; you can addT, reading givesObject.- PECS: Producer Extends, Consumer Super.
- Do not use wildcards in return types; they burden callers.
Draft outline details
Demonstration
static <T> void copy(List<? extends T> src, List<? super T> dst) {
for (T t : src) dst.add(t);
}Check your understanding
What does PECS stand for?
Show answer
Producer Extends, Consumer Super.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.