Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Why were default methods added in Java 8, and what risks do they bring when a library adds one to a widely implemented interface?

Default methods were added in Java 8 so the JDK could add methods such as Collection.stream and Iterable.forEach to existing interfaces without breaking every class that already implemented them. The risks when a library adds one are that an implementing class may already have a method with the same signature, which then overrides the default with possibly different meaning; that the default may be wrong for some implementations, as Collection.removeIf would be for synchronized wrappers, which therefore override it; and that two interfaces adding the same default force implementers to resolve the conflict. Defaults also cannot use state, so they are written in terms of the interface's other methods.

Why this question is asked

Before Java 8, adding a method to a public interface was effectively impossible: every class implementing it, in every library and application, would stop compiling. That froze the collections framework just when lambdas arrived and the JDK needed stream() and forEach(). Default methods were the solution, and they are now part of how every library evolves. Interviewers ask about the risks because a new default on a widely implemented interface is a subtle API change that can silently alter behaviour in code we do not own. In this answer, we will see the problem defaults solved, how a default is written, and then the three main risks, each with an example and a mitigation.

1The Problem Default Methods Solved

An interface is a contract that many independent classes implement. If the JDK had added an abstract stream() method to Collection in Java 8, every custom collection ever written, in Guava, in Hibernate, in thousands of applications, would have failed to compile against Java 8, and old compiled classes would have thrown AbstractMethodError when stream() was called on them.

A default method is an interface method with a body. Implementing classes inherit it automatically, so existing classes gain the new method without any change and old binaries keep working. That is how Java 8 could add Collection.stream(), Iterable.forEach(), Collection.removeIf(), List.sort() and Map.getOrDefault() to interfaces that had been stable for over fifteen years.

2Defaults Are Built From the Interface's Own Methods

A default cannot use state, because interfaces have no instance fields, so it must be written in terms of the other methods of the interface. Here is roughly how Iterable.forEach and Collection.removeIf are implemented.

Java
public interface Iterable<T> {
Iterator<T> iterator();
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) action.accept(t); // uses iterator()
}
}
// In Collection
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) { each.remove(); removed = true; } // uses Iterator.remove
}
return removed;
}

This works for every collection, but it is a general algorithm. It knows nothing about how a particular class stores its data or protects it, which is where the risks start. ArrayList, for instance, overrides removeIf with a faster version that shifts elements once instead of removing them one by one.

3Risk 1: A Method With the Same Signature Already Exists

When a default is added, some implementing class somewhere may already have a method with exactly that signature, written years earlier for its own purposes. The rule is that a class method always wins over an interface default, so that old method silently becomes the implementation of the new interface method, with whatever meaning it had.

Java
// Written in 2012, long before Java 8
class AuditLog implements Collection<Event> {
// Removes matching events AND writes an audit record for each removal
public boolean removeIf(Predicate<? super Event> p) { /* ... */ return true; }
// ... other Collection methods ...
}
Collection<Event> events = new AuditLog();
events.removeIf(e -> e.expired()); // Java 8+: generic code now calls AuditLog's method,
// which also writes audit records nobody expected

Library code that calls removeIf on any Collection now triggers the side effect. If the old method had a different return type, the result would be a compile error instead, which is at least visible. The silent case is worse. The mitigation for library authors is to search for existing methods with that name before adding a default, choose distinctive names, and document the new method's contract clearly so implementers can check theirs.

4Risk 2: The Default Is Wrong for Some Implementations

A default has to work for every implementation, but some implementations have rules the default cannot know about. Synchronized wrappers are the classic case. Collections.synchronizedCollection returns a wrapper that locks a mutex around each method. The inherited removeIf default would call iterator(), hasNext() and remove() as separate calls, so another thread could change the collection in between, and the whole operation would not be atomic.

Step throughremoveIf on a synchronized wrapper, if it used the default
Thread ASynchronized wrapperThread B
iterator() - lock taken and released
next(), test, remove() - not locked as one step
add(x) - slips in between
next() - ConcurrentModificationException or wrong result

4 messages in order. Play them, or step through them one at a time.

Each call is safe alone, but the sequence is not; the JDK's wrapper overrides removeIf to hold the lock for the whole loop.

That is why the JDK's synchronized wrappers override removeIf, forEach and the other new defaults to hold the lock for the entire operation, and why ConcurrentHashMap overrides Map defaults such as computeIfAbsent and merge to make them atomic. Implementations outside the JDK had to discover this themselves. So when a widely implemented interface gains a default, every implementation with special concurrency, persistence or validation rules should be reviewed to see whether the generic algorithm is correct for it.

5Risk 3: Two Interfaces Add the Same Default

If a class implements two interfaces and both libraries add a default with the same signature, the class no longer compiles: Java refuses to pick one, and the class must override the method and choose.

Java
interface Exportable { default String describe() { return "export"; } } // added in lib-a 2.0
interface Auditable { default String describe() { return "audit"; } } // added in lib-b 3.0
class Invoice implements Exportable, Auditable { }
// compile error: Invoice inherits unrelated defaults for describe() from types Exportable and Auditable
class Invoice implements Exportable, Auditable {
@Override public String describe() { return Exportable.super.describe(); } // export
}

This one fails loudly at compile time, which is better than a silent change, but it can still break a build after an innocent dependency upgrade in code that neither library author knew about.

6Follow-up questions

Is adding a default method binary compatible?

Yes, in the sense that existing compiled classes still link and run, which was the point of the feature. But as we saw, binary compatibility does not mean behavioural compatibility: an existing method can silently take over, and a generic default may be wrong for a specialised implementation.

How should a library team handle adding a default to a popular interface?

Treat it as an API change that needs review. Search known implementations for clashing signatures, document the default's contract including whether implementations should override it, call it out in release notes, and consider whether a static helper method would serve the need without touching every implementer.

Why can a default not just keep a little state in a static field?

A static field would be shared by every implementation and every instance, so it cannot represent per-object state, and it would introduce thread-safety problems. That is why defaults are restricted to combining the interface's abstract methods, and why stateful behaviour still belongs in classes.

7Conclusion

In this answer, we saw that default methods let Java 8 add stream(), forEach(), removeIf() and other methods to old interfaces without breaking existing implementations. Defaults have no state, so they are written in terms of the interface's abstract methods. When a library adds one, an existing method with the same signature may silently override it with different meaning, the generic default may be wrong for implementations like synchronized wrappers, which override removeIf to stay atomic, and two interfaces adding the same default force implementers to resolve a compile error.

What to say in the interviewAbout 80 seconds aloud

Open with

Default methods were added in Java 8 mainly so the JDK could evolve its interfaces. Lambdas needed things like Collection.stream, Iterable.forEach and removeIf, but adding an abstract method to Collection would have broken every collection class ever written.

Then, if they want more

A default has a body, so existing implementations inherit it and keep compiling and running. Because interfaces have no fields, a default is written in terms of the other interface methods; removeIf, for example, is a loop using iterator and Iterator.remove. The risks come when a library adds a default to a widely implemented interface.

First, an implementing class may already have a method with the same signature, and since class methods win over defaults, it silently becomes the implementation, maybe with different meaning or side effects. Second, the generic default can be wrong for some implementations.

A synchronized wrapper using the default removeIf would lock each call separately, so the whole operation wouldn't be atomic; that's why Collections.synchronizedCollection overrides it, and ConcurrentHashMap overrides computeIfAbsent and merge. Third, if two interfaces a class implements both add the same default, the class stops compiling until it overrides the method and picks one with Interface.super.

So I treat a new default on a popular interface as an API change that needs review, not a free addition.

About this answer

Last updated
12 November 2026
Versions it names
Java 8
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 "Why were default methods added in Java 8, and what risks do they bring when a library...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/ec5c979dc5/