It is claimed in this article that:

one of the major reasons for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.

I could understand that the @FunctionalInterface helped by saying that there is ONLY one abstract method and the lambda should represent this particular method.

But how is it that the default methods helped to support lambdas?


To give you an example take the case of the Collection.forEach method, which is designed to take an instance of the Consumer functional interface and has a default implementation in the Collection interface:

default void forEach(Consumer<? super T> action) {
    for (T t : this) {

If the JDK designers didn't introduce the concept of default methods then all the implementing classes of the Collection interface would have to implement the forEach method so it would be problematic to switch to Java - 8 without breaking your code.

So to facilitate the adoption of lambdas and the use of the new functional interfaces like Consumer, Supplier, Predicate, etc. the JDK designers introduced the concept of default methods to provide backward compatibility and it is now easier to switch to Java - 8 without making any changes.

If you don't like the default implementation in the interface you can override it and supply your own.

  • Adding default methods to java interfaces probably did brake existing java code, for example: if a class in java 7 has an int sort() method and also implements Collection, and now there is a default void sort() method in Collection, then the existing class will no longer compile in java 8. I guess that the JDK designers took that into account and chose the best option they had at the time. – dorony Apr 26 at 17:35

They helped indirectly: you can use lambdas on collections thanks to additional methods like removeIf(), stream(), etc.

Those methods couldn't have been added to collections without completely breaking existing collection implementations if they had not been added as default methods.


Another situation where default methods help a ton is in the functional interfaces themself. Take the Function<T,R> interface for example, the only method you really care about is R apply(T t), so when you need a Functionsomewhere, you can pass a lambda and it will create a Function instance where that lambda method is the apply method.

However once you have a Function instance, you can call other useful methods like <V> Function<T,V> andThen(Function<? super R,? extends V> after) that combine functions on them. The default implementation is simply chaining the functions, but you can override it if you create your own class implementing the Function interface.

In short, default methods give you an easy way to create lambdas from functional interfaces that have additinal methods, while giving you the option to override those additinal methods in with a full class if you need to.

  • In that case an abstract class would've achieved the same thing (though removing the opportunity to extend another class). – OrangeDog Apr 22 at 9:04
  • I consider this as the most crucial point (At least referring to the quoted statement and question title on a language level - not so much on the (collections) API level) : Namely, that you can have an interface with multiple methods, but still "implement" it via a lambda expression if and only if it has a single abstract method (in fact, in the beginning, these types had been referred to as "SAM Types" or "Single Abstract Method Types"). – Marco13 Apr 22 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.