Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

A ReadOnlyList class extends a mutable List implementation and throws UnsupportedOperationException from add. Is that a Liskov violation? How do the JDK's own collections handle this?

It is a Liskov problem in spirit: callers that expect add to work will fail at run time, so the subclass weakens the behaviour they rely on. The JDK technically allows it, because List documents add and remove as optional operations that may throw UnsupportedOperationException, and its own unmodifiable lists work that way. Java chose not to have a read-only list type to keep the collection API small, so in our own APIs we should separate read and write interfaces and document when a method returns an unmodifiable view.

Why this question is asked

Read-only collections come up constantly in real work: a service returns a list of prices that callers must not change, or a configuration object exposes its entries. Many teams write a ReadOnlyList that extends ArrayList and overrides add to throw. Interviewers like this question because it sits right on the line between a clean design and a pragmatic compromise, and because the JDK itself made that compromise. In this answer, we will look at what List promises, build the ReadOnlyList and see where it leaks, explain how the JDK's own unmodifiable lists behave and why there is no read-only list interface, and finish with how to design our own APIs so the types say what callers can do.

1A Quick Recap of What List.add Promises

The Liskov substitution principle says code written against a type must keep working with any subtype. So the question is what the List type actually promises. Its documentation describes add, remove, set, clear and similar methods as optional operations. Each of them states that it may throw UnsupportedOperationException if the list does not support that operation.

So, on paper, a list that throws from add does not break the List contract, because the contract allows it. But most code is written by people who expect add to work, and the type system gives them no way to know otherwise. That gap between what the documentation allows and what callers expect is the heart of this question.

2Building ReadOnlyList and Watching It Leak

Here is the subclass. It looks safe, but extending a mutable implementation gives us every mutating method of ArrayList, and overriding one of them is not enough.

Java
class ReadOnlyList<E> extends ArrayList<E> {
ReadOnlyList(Collection<? extends E> items) { super(items); }
@Override public boolean add(E e) { throw new UnsupportedOperationException("read-only"); }
}
List<String> names = new ReadOnlyList<>(List.of("ann", "bob"));
names.add("cy"); // UnsupportedOperationException: read-only
names.addAll(List.of("cy")); // true: ArrayList.addAll does not call add
names.remove("ann"); // true
names.set(0, "zed"); // returns "bob"
System.out.println(names); // [zed, cy]

So the class fails in two directions at once. It surprises honest callers that call add, and it does not stop anyone who calls addAll, remove, set, clear, removeIf, sort or iterator().remove(). To be truly read-only, it would have to override every mutator of ArrayList and keep up with new ones added in later Java versions. That is the fragile base class problem: our subclass depends on how ArrayList implements its methods internally, which we do not control.

Notice also that ReadOnlyList is an ArrayList, so it can be passed to any method that takes an ArrayList, which certainly expects to mutate it. That is the clearest Liskov failure: the subtype is less capable than its supertype in a way the type does not reveal.

3How the JDK Handles Read-Only Lists

The JDK never makes a read-only list by subclassing a mutable one. It returns separate implementations of List that throw from every mutator, and it gives us several flavours depending on what we need.

Read-only lists in the JDK
FactoryWhat it isBehaviour to know
Collections.unmodifiableList(l)A view over lMutators throw; changes to l show through
List.of(...)Immutable listMutators throw; nulls rejected
List.copyOf(c)Immutable copySnapshot; nulls rejected
stream.toList()Unmodifiable listMutators throw; nulls allowed
Arrays.asList(...)Fixed-size view of an arrayset works; add and remove throw
All of these are plain List types at compile time; only the documentation says what they support.

The difference between a view and a copy matters in practice. An unmodifiable view stops the caller from changing the list, but if the owner keeps changing the backing list, the caller sees those changes, which can surprise code that iterates later. List.copyOf takes a snapshot that never changes. Arrays.asList is the odd one out: it is fixed-size but not read-only, so set works and add throws.

4Why Java Has No Read-Only List Interface

A cleaner design would split List into a ReadableList with get, size and iteration, and a MutableList that extends it with add and remove. Kotlin, for example, has that split at the type level. Java's collection designers chose not to. With separate interfaces for every combination of read-only, fixed-size, append-only and fully mutable, across List, Set, Map and their sorted and sequenced variants, the hierarchy would multiply into dozens of interfaces.

So the trade-off was a smaller, simpler API in exchange for run-time errors instead of compile-time ones. That is why UnsupportedOperationException exists. It is a known compromise, and it was a reasonable one for a general-purpose library. But it does not mean our own domain APIs should copy it.

5Designing Our Own APIs to Say What Callers Can Do

In our own code, we have far fewer types and we can afford to be precise. If some clients only read and others write, we give them different interfaces, so the compiler, not a run-time exception, tells a reader that they cannot write.

Java
public interface PriceBook { // what readers may do
Optional<Money> priceOf(Sku sku);
List<Sku> skus(); // returns an unmodifiable copy; documented below
}
public interface EditablePriceBook extends PriceBook {
void setPrice(Sku sku, Money price);
}
/** Returns an unmodifiable snapshot of the SKUs; later price changes are not reflected. */
@Override public List<Sku> skus() { return List.copyOf(prices.keySet()); }

Reporting services receive a PriceBook, and only the admin flow receives an EditablePriceBook. When we must return a JDK List, we say so in the method's documentation: whether it is unmodifiable, whether it is a view or a snapshot, and whether it may contain nulls. That turns the optional-operation compromise into an explicit part of our contract instead of a surprise.

6Follow-up questions

Is it acceptable to return Collections.unmodifiableList from a getter?

Yes, and it is a good default for exposing internal state, because callers cannot change our list. We should document that it is a live view, so callers know that later changes by the owner will be visible. If callers need a stable snapshot, List.copyOf is the better choice.

Why does List.of reject null elements?

The immutable collections were designed to reject nulls so that a null never hides inside a collection and fails much later. It also lets methods like contains(null) be well defined. If nulls are part of the data, Collections.unmodifiableList or stream().toList() allow them.

Would composition fix the ReadOnlyList class?

It would fix the leaks. A class that wraps a list and exposes only get, size and iteration cannot be mutated through methods it never offers. If it must be a List, extending AbstractList and implementing only get and size gives a list whose mutators all throw, because AbstractList's defaults do.

7Conclusion

In this answer, we saw that a ReadOnlyList extending ArrayList surprises callers who expect add to work and still lets them mutate the list through addAll, remove and set. The JDK documents mutators as optional operations, so its own unmodifiable lists are technically allowed, but they are separate implementations, not subclasses, and they still surprise callers because Java deliberately has no read-only list type. In our own APIs we can separate read and write interfaces and document clearly when we return an unmodifiable view or copy.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd say it's a Liskov violation in practice even if it's allowed on paper. The List interface documents add and remove as optional operations that may throw UnsupportedOperationException, so the JDK's own unmodifiable lists are within the contract.

Then, if they want more

But callers written against List or, worse, ArrayList expect add to work, and the types give them no warning, so they fail at run time. Extending ArrayList also leaks: if I only override add, then addAll, remove, set, clear and iterator remove still mutate the list, because ArrayList's addAll doesn't go through add. It's the fragile base class problem.

The JDK never does it this way. It returns separate List implementations: Collections.unmodifiableList, which is a view where the owner's changes show through, List.of and List.copyOf, which are immutable and reject nulls, and Stream.toList.

Java chose not to have a read-only list interface because splitting every collection type into readable and mutable versions would multiply the hierarchy, so it accepted run-time errors for a smaller API. In my own code I don't copy that compromise.

If some clients only read, I give them a read interface and the writers a sub-interface with the mutators, so the compiler tells people what they can do. And when I return a JDK list, I document whether it's unmodifiable and whether it's a live view or a snapshot.

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 "A ReadOnlyList class extends a mutable List implementation and throws...": https://scrivio-interview-library.netlify.app/java/oop-principles/9d056ae0af/