Java 8 has a lot of improvements, many of them touch interfaces. The problem is when new release want to add new methods to old interfaces from standard library. Every class implementing that interface should be rewrited to add implementations of missing methods. That’s very big problem – unacceptable in Java.

Java 8 provide solution for problem of this kind – default methods.

Java designers decided to define methods with concrete implementations in interfaces. This methods are called default methods. Those methods can be safely added to existing interfaces.

Consider the interface:

The interface has two methods: the abstract one getDisplayName() and the default one getName() – a concrete class that implements the Person interface must provide an implementation of getDisplayName(), but it can choose to keep the implementation of getName() or to override it.

There is risk to implement two (or even more) interfaces with the same name of default method. What’s then?

For example:

But which default getName() compilator’ll choose? No one In such case you’ll get an error that class implements few unrelated default methods with the same name. You should override this method and add your own implementation.

If you prefer to choose implementation from interface you have to define it manually. Example below shows how to choose Person‚s default method.

Second case is when class implement interface with default method and extend superclass which implement another interface with method of the same name.
What’s then? It depends. Main rule is one: „class’ implementations always wins”. If superclass overrides the method, than subclass use the implemtation. If superclass doesn’t override but use default method from interface, then there is interface clash and this method should be overriden with its own implementation.

Static methods in interfaces

As of Java 8, you are allowed to add static methods to interfaces. Up to now, it has been common to place static methods in utility (companion) classes. In standard library you’ll find a lot of pairs such classes, for example: Array/Arrays, Collection/Collections or Path/Paths. Now you can all static methods put into interface class.
.