Quick revision
The short answer
Java 21 added getFirst, getLast, addFirst, addLast, removeFirst, removeLast and reversed to List and Deque as default methods, through the new SequencedCollection interface. A class that implements both List and Deque now inherits two reversed() defaults with different return types, List<E> and Deque<E>, so it must override reversed() itself, as LinkedList does. A class that already had a method with one of these names but an incompatible return type no longer compiles, and one whose method happened to match now silently overrides the new default, which can change behaviour.
What to remember
In this answer, we saw that Java 21 added sequenced methods to List and Deque as defaults. A class implementing both inherits two reversed() defaults with incompatible return types, and must override reversed() with a type that is both, as LinkedList does. A class with an existing getFirst or similar method of an incompatible return type fails to compile and must rename it, while a method with a matching signature now silently overrides the default and can change behaviour. Reading the release notes and searching for these names before upgrading catches all of it.
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.
Java 21 added getFirst, getLast, addFirst, addLast, removeFirst, removeLast and reversed to List and Deque as default methods, through the new SequencedCollection interface. A class that implements both List and Deque now inherits two reversed() defaults with different return types, List<E> and Deque<E>, so it must override reversed() itself, as LinkedList does. A class that already had a method with one of these names but an incompatible return type no longer compiles, and one whose method happened to match now silently overrides the new default, which can change behaviour.
Why this question is asked
Java prides itself on backward compatibility, so a compile failure after an upgrade surprises people. Adding methods to widely implemented interfaces is one of the few changes that can break source code, and sequenced collections did exactly that for some projects. Interviewers ask about it because explaining it needs a clear model of default methods, overriding and return types, and because the same thing can happen to any interface we publish. In this answer, we will recap how default methods are inherited, reproduce both failures, explain the rules behind them, look at the case where code compiles but behaves differently, and fix each one.
1A Quick Recap of Default Methods
A default method is a method with a body declared in an interface. It lets the JDK add methods to an interface without breaking the classes that implement it: those classes simply inherit the default. This is how Java 21 added getFirst and its relatives to List and Deque without every collection library having to change.
There are two catches. First, if a class already declares a method with the same name and parameters, that method now overrides the default, and its return type must be compatible: the same type, or a subtype. If it is not, the class no longer compiles. Second, if a class inherits two defaults with the same signature from two unrelated interfaces, and neither is more specific, the compiler refuses to choose, and the class must override the method itself.
2Failure 1: A Class That Is Both a List and a Deque
Some projects have a collection that is a List, for index access, and a Deque, for cheap operations at both ends. It compiled fine on Java 17.
- SequencedCollection<E>declares abstract
SequencedCollection<E>reversed() - List<E> and Deque<E>each adds a default:
List<E>reversed()andDeque<E>reversed() - RingBuffer<E>inherits both; neither is more specific, so it must override
3 layers, from the top down. Step through them one at a time.
The fix is an override whose return type is both a List and a Deque.
In Java 21, List declares default List<E> reversed() and Deque declares default Deque<E> reversed(). Both override SequencedCollection's reversed(), but neither interface extends the other, so for RingBuffer neither default is more specific. The return types also differ, and no single inherited method returns something that is both a List and a Deque. The compiler cannot pick, so it stops. The exact error wording varies between compiler versions, but it always names reversed() and the two interfaces.
3The Fix: Override reversed() Like LinkedList Does
The JDK had the same problem with its own LinkedList, which is both a List and a Deque. LinkedList solves it by overriding reversed() with a return type of LinkedList<E>. That is legal because a method may override with a more specific return type, a covariant return, and LinkedList<E> is a subtype of both List<E> and Deque<E>. We do the same.
The contract of reversed() asks for a view that reflects the original, not a copy, so the proper implementation is a small subclass that maps index i to size() - 1 - i and swaps the meaning of first and last. Returning a reversed copy also compiles, but it breaks the contract: callers who modify the original would not see it in the result. If the class does not truly need to be both types, the simpler fix is to stop implementing one of them.
4Failure 2: An Existing Method With the Same Name
The second failure comes from a custom list that already had a getFirst method, but returning a different type, often an Optional.
On Java 21, ArrayList has a getFirst() that returns E. Batch declares a method with the same name and no parameters, which Java treats as an attempt to override, and an override must return E or a subtype of E. Optional<E> is neither, so the compiler rejects it. The fix is to rename our method, for example to firstIfPresent(), and update its callers; the new standard getFirst covers the other use.
5When It Compiles but Behaves Differently
The quieter case is a method that happens to match the new signature exactly. Suppose a project's list class had E getFirst() that returned null for an empty list, a choice made years ago. On Java 21 it compiles without a warning, and it now overrides List.getFirst(). Any code that calls getFirst() through a List reference, including library code that expects NoSuchElementException on an empty list, now gets null instead. The opposite also happens: a class that defined removeLast() to mean something unusual, such as removing the last element added rather than the last in order, now provides that meaning to every caller using the standard interface.
This also applies to classes compiled on Java 17 and run on Java 21 without recompiling. The JVM matches methods by name and by erased descriptor, so a compiled E getFirst() matches the new default and overrides it at run time. A method returning Optional has a different descriptor, so it does not, and calls through List get the JDK's default instead.
That is why the last step of any upgrade is reading the release notes. The JDK 21 notes for JEP 431 list this exact source incompatibility, and searching our own code for classes that implement List or Deque, or extend the collection classes, and declare methods with the new names, finds every candidate in minutes.
6Follow-up questions
Why did the JDK not just give one of the defaults priority?
The Java rules for default methods are deliberately simple: a class's own method wins, then the most specific interface wins, and if there is still a tie, the programmer decides. Picking List or Deque silently would have given RingBuffer a reversed() whose return type is wrong for the other interface, so the compiler asks us to decide.
Does this affect Kotlin or other JVM languages?
It can. Kotlin, for example, has its own removeFirst and removeLast extension functions on mutable lists, and when compiling against a Java 21 JDK, calls may bind to the new JDK members instead, which then fail on older runtimes that lack them. The details depend on the Kotlin version, so check its release notes when upgrading.
How do I avoid causing the same problem with my own interfaces?
Prefer adding new methods to new interfaces or with names unlikely to clash, and treat adding a default method to a widely implemented public interface as a potentially breaking change that needs a release note.
7Conclusion
In this answer, we saw that Java 21 added sequenced methods to List and Deque as defaults. A class implementing both inherits two reversed() defaults with incompatible return types, and must override reversed() with a type that is both, as LinkedList does. A class with an existing getFirst or similar method of an incompatible return type fails to compile and must rename it, while a method with a matching signature now silently overrides the default and can change behaviour. Reading the release notes and searching for these names before upgrading catches all of it.
Open with
The cause is the sequenced collections change in Java 21. The JDK added getFirst, getLast, addFirst, addLast, removeFirst, removeLast and reversed to List and Deque as default methods, through the new SequencedCollection parent.
Then, if they want more
Default methods normally keep old classes compiling, but there are two cases where they do not. First, a class that implements both List and Deque inherits two reversed defaults, one returning List and one returning Deque. Neither interface is more specific, so the compiler refuses and the class has to override reversed itself.
The fix is what LinkedList does: override it with a covariant return type that is both, so LinkedList's reversed returns a LinkedList, and our class would return its own type, ideally a reverse view rather than a copy, since that is the contract.
Second, a custom list that already had a getFirst returning, say, Optional, now clashes with getFirst returning E, so it fails with an incompatible return type, and we rename our method. The subtle case is a method that matches exactly, like an old getFirst that returns null for an empty list.
It compiles and now overrides the standard method, so callers through the List interface get different behaviour. I would read the JDK 21 compatibility notes and search for classes that implement List or Deque and declare these method names before upgrading.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 17
- 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 Collections Framework, overview, java.util, package 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 "After upgrading to Java 21, a project fails to compile with a conflict in a class that...": https://scrivio-interview-library.netlify.app/java/collections-overview/d708a3316f/