Skip to content
Ameen Abozaid
Planned lessonMID-009-L02

Wildcards and PECS

  • Lesson 2 of 6
  • Mid-level
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 a List<Number>: generics are invariant.
  • ? extends T: a producer; you can read T, not add.
  • ? super T: a consumer; you can add T, reading gives Object.
  • PECS: Producer Extends, Consumer Super.
  • Do not use wildcards in return types; they burden callers.
Draft outline details

Demonstration

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

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.