Quick revision
The short answer
The most flexible signature is <T> void addAll(List<? super T> target, List<? extends T> source). The compiler infers T, typically as the source's element type such as Integer, and then target can be a List<Integer>, List<Number> or List<Object>, while source can be a list of T or any subtype of T. This is the same shape as Collections.copy and Collections.addAll(Collection<? super T>, T...), and it follows PECS: the source produces values, so extends, and the target consumes them, so super.
What to remember
In this answer, we saw that addAll(List<Number> target, List<Integer> source) rejects safe calls because generic types are invariant. Generalizing the element type to T and applying PECS gives <T> void addAll(List<? super T> target, List<? extends T> source), which accepts List<Integer>, List<Number> and List<Object> targets and sources of any subtype of T, while inference still rejects unsafe combinations. The JDK's Collections.copy and Collections.addAll use the same shape.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The most flexible signature is <T> void addAll(List<? super T> target, List<? extends T> source). The compiler infers T, typically as the source's element type such as Integer, and then target can be a List<Integer>, List<Number> or List<Object>, while source can be a list of T or any subtype of T. This is the same shape as Collections.copy and Collections.addAll(Collection<? super T>, T...), and it follows PECS: the source produces values, so extends, and the target consumes them, so super.
Why this question is asked
A signature that names exact generic types often looks right until somebody tries to call it with a slightly different list. Because generic types are invariant, List<Number> accepts only a List<Number>, which rules out perfectly safe calls. Interviewers ask this debugging question because rewriting the signature correctly shows a practical grasp of wildcards, inference and PECS. In this answer, we will reproduce why the original signature rejects valid calls, rewrite it step by step, see how inference picks T, and compare the result with the JDK.
1A Quick Recap of Why Exact Types Are Rigid
Generic types are invariant: List<Integer> is not a List<Number>, and List<Object> is not a List<Number> either. With a parameter declared as List<Number>, only an argument whose type is exactly List<Number> is accepted. That is safe, but often stricter than it needs to be.
Wildcards loosen this in a controlled way. ? extends T accepts lists of T or its subtypes and is safe for reading. ? super T accepts lists of T or its supertypes and is safe for writing T values. The skill is to pick, for each parameter, the loosest form that the method body still allows.
2Reproducing the Problem
Here is the original method and the two calls that fail.
Both failing calls are safe in reality. Adding Integers to a List<Object> is fine, since an Integer is an Object, and adding Integers to a List<Integer> is obviously fine. They fail only because the signature fixes target's type to exactly List<Number>. The method also only works with Integer sources, so it cannot copy Longs or Doubles, even though the body does nothing Integer-specific.
3Rewriting the Signature Step by Step
First, we generalize the element type. The body does not care that the elements are Integers; it only moves them. So we introduce a type parameter T for the element type: <T> void addAll(List<T> target, List<T> source). Now any matching pair works, but both lists still need exactly the same type.
Next, we apply PECS to each parameter. source is only read, so it is a producer and becomes List<? extends T>. target is only written to, so it is a consumer and becomes List<? super T>. That gives the final signature.
| Call | (List<Number>, List<Integer>) | <T> (List<? super T>, List<? extends T>) |
|---|---|---|
addAll(nums, ints) | yes | yes |
addAll(objs, ints) | no | yes |
addAll(moreInts, ints) | no | yes |
addAll(nums, doubles) | no | yes |
addAll(ints, nums) | no | no: Numbers cannot go into a List<Integer> |
Notice that the body type-checks for the reasons PECS describes: each x read from List<? extends T> is a T, and adding a T to a List<? super T> is always safe. All three calls from before compile, and the method now works for any element type.
4How Inference Picks T
When we call addAll(objs, ints), the compiler has to find a T that satisfies both parameters. From source, List<Integer> must be a List<? extends T>, so Integer must be a subtype of T. From target, List<Object> must be a List<? super T>, so T must be a subtype of Object. Any T between Integer and Object would work, and the compiler picks Integer, the most specific type that satisfies the lower bound.
The final row of the table shows the rules still protect us. For addAll(ints, nums), source requires Number to be a subtype of T, and target requires T to be a subtype of Integer. No type is both a supertype of Number and a subtype of Integer, so inference fails and the call is rejected, which is correct, because putting a Double into a List<Integer> would corrupt it.
- From source: List<Integer> fits List<? extends T>so Integer is a subtype of T
- From target: List<Object> fits List<? super T>so T is a subtype of Object
- Is there a T between Integer and Object?YesPick the most specific: T = IntegerNoCompile error: no valid T
3 steps in order. Play them, or step through them one at a time.
5The Same Shape in the JDK
This is not a trick invented for one method. Collections.copy(List<? super T> dest, List<? extends T> src) has exactly this shape, and Collections.addAll(Collection<? super T> c, T... elements) uses ? super T for the destination with a varargs source. Stream.collect, Comparator.comparing and many other library methods follow the same pattern. When designing our own utilities, looking at how the JDK declares a similar method is a reliable way to get the wildcards right.
One more improvement is worth considering: if the method does not need indexes or order, it can take Collection<? super T> and Iterable<? extends T>, which accept sets and other collections too.
6Follow-up questions
Why not use List<? super T> for both parameters?
Because then reading from source would only give Object, and the body could not add those values to target. Each parameter needs the wildcard that matches what the method does with it: extends for reading, super for writing.
Do we need the type parameter T at all?
Yes. T links the two parameters: it says the source's elements are suitable for the target. Without it, there would be no way to express that relationship, and the method could not be written safely with only unbounded wildcards.
Does this change break existing callers?
No source-compatible caller breaks, because every call the old signature accepted is still accepted. It is also binary compatible, since both signatures erase to addAll(List, List).
7Conclusion
In this answer, we saw that addAll(List<Number> target, List<Integer> source) rejects safe calls because generic types are invariant. Generalizing the element type to T and applying PECS gives <T> void addAll(List<? super T> target, List<? extends T> source), which accepts List<Integer>, List<Number> and List<Object> targets and sources of any subtype of T, while inference still rejects unsafe combinations. The JDK's Collections.copy and Collections.addAll use the same shape.
Open with
The original signature is too rigid because generic types are invariant: List<Number> accepts only a List<Number>, so passing a List<Object> or a List<Integer> as the target fails, even though adding Integers to either is safe, and the source is stuck as Integer.
Then, if they want more
I would rewrite it as <T> void addAll(List<? super T> target, List<? extends T> source), with the body for (T x : source) target.add(x). First I generalize the element type to T, since the method only moves values.
Then I apply PECS: source is only read, so it is a producer and gets ? extends T, and target is only written, so it is a consumer and gets ? super T.
The compiler infers T from the call, for example with an Integer source it infers T as Integer, and then the target can be a List<Integer>, a List<Number> or a List<Object>, and the source can be a list of T or any subtype.
Unsafe calls are still rejected, like adding a List<Number> into a List<Integer>, because no T fits both bounds. This is exactly the shape the JDK uses for Collections.copy and for Collections.addAll with Collection<? super T>. The change is source compatible for existing callers and binary compatible, since the erasure is still addAll(List, List).
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A method declared as void addAll(List<Number> target, List<Integer> source) cannot be...": https://scrivio-interview-library.netlify.app/java/generics/da37c2d6be/