Defaults

Learn about interface default methods.

Default methods

To add the stream method (or any others) to the core Collections API, Java needs another new feature, Default methods (also known as Defender Methods or Virtual Extension methods). For example, they could add new methods to the List interface without breaking all the existing implementations, known as backward compatibility.

Default methods can be added to any interface. As the name implies, any class that implements the interface but does not override the method will get the default implementation.

For example, the stream method in the Collection interface is defined something like the following:

default public Stream stream() {
   return StreamSupport.stream(spliterator());
}
...