ProLesson 6.4Advanced3 min readUpdated 30 October 2026
Wildcards: ? extends, ? super and PECS
Why a List<Integer> is not a List<Number>, how wildcards make methods flexible, and the PECS rule interviewers ask about.
Wildcards are the trickiest part of generics, but one rule makes them manageable. First, a surprise.
01List is not a List
Integer is a Number, so you'd expect a List<Integer> to be a List<Number>. It isn't:
java
List<Integer> ints = new ArrayList<>(List.of(1, 2));
List<Number> nums = ints; // doesn't compileIf it were allowed, this would happen:
java
nums.add(3.14); // a Double in a list of Integers!
int x = ints.get(2); // crashSo generic types are invariant: List<Integer> and List<Number> are unrelated types. Wildcards are how you get flexibility back, safely.
Loading the rest of the lesson…