Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

You are reviewing a shared library's public API with signatures such as void process(List<Event> events) and List<? extends Event> fetch(). What wildcard guidelines would you apply?

For parameters, we use wildcards for flexibility: process(Collection<? extends Event> events) accepts lists and sets of any Event subtype, and functional parameters that consume values take ? super, as in Consumer<? super Event> or Comparator<? super T>. For return types, we avoid wildcards, so fetch() should return List<Event>, not List<? extends Event>. We prefer Collection or Iterable over List when order and indexing are not needed. Changing List<Event> to List<? extends Event> keeps the erasure, so it is binary compatible and source compatible for most callers, but changing List to Collection changes the erasure and needs a new major version or an overload.

Why this question is asked

A shared library's signatures are copied into every team's code. A parameter that is too strict forces callers to build copies of their lists just to satisfy the compiler, and a return type with a wildcard makes every caller deal with an unknown type. Fixing these after release is harder than getting them right, because changes have to stay compatible. Interviewers ask this design question to see whether we know the wildcard guidelines and also their compatibility consequences. In this answer, we will recap why wildcards matter in APIs, then go through parameters, return types, functional interfaces, choosing collection types, and how to change existing signatures safely.

1A Quick Recap of Why Wildcards Matter in APIs

Generic types are invariant, so a parameter of type List<Event> accepts only a List<Event>. If a caller has a List<OrderEvent>, where OrderEvent extends Event, it cannot pass it, even though process only reads events. The caller has to copy the list into a new List<Event> first, which is wasteful and puzzling.

A bounded wildcard fixes this. List<? extends Event> accepts a list of Event or any subtype. The PECS rule, producer extends and consumer super, tells us which wildcard to use: extends for parameters we read from, super for parameters we write into or pass values to.

2Parameters: Use Wildcards for Flexibility

Here is the reviewed method and the problem a caller hits.

Java
// before
public void process(List<Event> events) { for (Event e : events) handle(e); }
List<OrderEvent> orders = loadOrders();
processor.process(orders); // error: List<OrderEvent> cannot be converted to List<Event>
processor.process(new ArrayList<Event>(orders)); // the workaround callers end up writing
// after
public void process(Collection<? extends Event> events) { for (Event e : events) handle(e); }
processor.process(orders); // compiles
processor.process(Set.of(new PaymentEvent())); // compiles: a Set works too

The body only reads events, so ? extends Event costs nothing inside the method and opens it to every caller. Notice we also changed List to Collection, which we will come back to, because it has a compatibility consequence that the wildcard alone does not.

3Return Types: Avoid Wildcards

fetch() currently returns List<? extends Event>. That pushes the uncertainty onto every caller. They cannot add to the list, they cannot pass it to a method that expects List<Event>, and they often end up casting or copying.

Java
// before
public List<? extends Event> fetch() { ... }
List<? extends Event> got = repo.fetch();
got.add(new AuditEvent()); // error: cannot add to ? extends Event
List<Event> mine = repo.fetch(); // error: List<CAP#1> cannot be converted to List<Event>
// after
public List<Event> fetch() { ... } // callers get a plain, usable type
Wildcard guidelines for a public API
PositionGuidelineExample
Parameter we read from? extends Tprocess(Collection<? extends Event>)
Parameter we write into? super TdrainTo(Collection<? super Event>)
Function that consumes T? super TforEach(Consumer<? super Event>)
Function that produces T? extends TSupplier<? extends Event>
Return typeno wildcardList<Event> fetch()
Collection typewidest that fitsIterable or Collection unless index or order matters

Effective Java puts it simply: do not use wildcards as return types. A wildcard in a parameter makes the method accept more; a wildcard in a return type makes the result harder to use, which helps nobody. If the library wants to stop callers modifying the returned list, it should return an unmodifiable List<Event>, such as one from List.copyOf, rather than rely on the wildcard.

4Functional Parameters and Comparators

Functional interfaces follow the same rule, applied to their own type arguments. A Consumer<T> accepts T values, so a parameter of type Consumer<? super Event> lets callers pass a Consumer<Object> or Consumer<Event>, for example a general-purpose logger. A Comparator<T> compares T values it receives, so sort methods take Comparator<? super T>, which is what lets a Comparator<Event> sort a list of OrderEvents. A Function<T, R> consumes T and produces R, so a flexible parameter is Function<? super T, ? extends R>, the shape Stream.map uses.

These are easy to miss in review because the strict version compiles fine for the library's own tests. The problem only appears when a caller with a slightly different type tries to use it, so a useful review question is: could a caller reasonably have a supertype consumer or a subtype producer here?

5Choosing Collection, Iterable or List

Wildcards make the element type flexible; the container type deserves the same thought. If process only iterates over the events, Iterable<? extends Event> or Collection<? extends Event> accepts lists, sets, queues and other collections. Requiring List says the method needs order or indexing, which is a stronger demand and should be made only when it is true. Collection is a common middle ground, because it also offers size() and stream().

6Changing Existing Signatures Compatibly

Since the library is already in use, each change needs a compatibility check. Binary compatibility means already-compiled callers keep working without recompiling. Source compatibility means callers still compile when they recompile against the new version.

Changing List<Event> to List<? extends Event> is binary compatible, because both erase to List, so the method's descriptor in the class file is unchanged. It is source compatible for most callers, since everything they could pass before they can still pass. The exception is a subclass that overrides process(List<Event>): after the change, its method has the same erasure but no longer overrides, and the compiler reports a name clash. Changing List to Collection is different: the erasure changes from List to Collection, so the descriptor changes and already-compiled callers fail with NoSuchMethodError. That change needs a new overload, process(Collection<? extends Event>), with the old method kept, deprecated and delegating, or a major version.

Step throughChecking a signature change for compatibility
  1. Does the erasure change? (List -> Collection)
    YesBinary incompatible: add an overload or bump majorNoBinary compatible
  2. Do subclasses override the method?
    YesCheck for name clashes; consider final or a new nameNoSource compatible for most callers
  3. Release with notes and test on consumer repositories

3 steps in order. Play them, or step through them one at a time.

Widening type arguments is usually safe; changing the raw container type is not.

7Follow-up questions

Is there ever a reason to return a wildcard type?

Rarely. One case is a method returning a Class<? extends T>, such as getClass(), where the exact type genuinely is unknown. For collections returned to callers, a concrete type argument is almost always better.

How would you enforce these guidelines in a large codebase?

Put them in the API review checklist, and use static analysis where it exists; some tools can flag wildcard return types or strict functional parameters. Most importantly, write tests that call the API with subtype lists and supertype consumers, which catch strict signatures before release.

Does ? extends Event in a parameter cost anything at run time?

No. Wildcards are a compile-time concept and are erased. The generated bytecode for process(List<Event>) and process(List<? extends Event>) is the same.

8Conclusion

In this answer, we reviewed a library API with generics guidelines. Parameters use wildcards following PECS, so process(Collection<? extends Event>) accepts any subtype collection and consumers such as Consumer<? super Event> and Comparator<? super T> accept supertypes. Return types avoid wildcards, so fetch() returns List<Event>. We prefer Collection or Iterable when order and indexing are not needed. Widening a type argument to a wildcard keeps the erasure and is binary compatible, but changing List to Collection changes the erasure and needs an overload or a major version.

What to say in the interviewAbout 80 seconds aloud

Open with

I would apply a few rules. For parameters, use wildcards following PECS. process only reads events, so it should take Collection<? extends Event>; then a caller with a List<OrderEvent> or a Set of events can pass it directly instead of copying into a List<Event>.

Then, if they want more

Parameters that consume values, like Consumer<? super Event> or Comparator<? super T>, should use super, so a general consumer or comparator of a supertype works. A Function parameter would be Function<? super T, ? extends R>.

For return types, I would avoid wildcards: fetch() returning List<? extends Event> makes every caller deal with an unknown type, they cannot add to it or pass it as List<Event>, so I would return List<Event>, unmodifiable if needed. I would also prefer Collection or Iterable over List when the method does not need order or indexing.

Then compatibility, because the library is already used: changing List<Event> to List<? extends Event> keeps the erasure, so it is binary compatible, and source compatible for most callers, though a subclass overriding the old signature would get a name clash.

Changing List to Collection changes the erasure, so already-compiled callers would get NoSuchMethodError; for that I would add an overload, deprecate the old method and delegate, or do it in a major version.

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.
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 "You are reviewing a shared library's public API with signatures such as void...": https://scrivio-interview-library.netlify.app/java/generics/5cb21dcd9e/