The implementation is simple: take an interface, add a concrete method and attach the keyword default as a modifier. The result is that suddenly all existing implementations of your interface can use this code. In this first, simple example, I’ve added default method that returns the version number of an interface1.
public interface Version {
/**
* Normal method - any old interface method:
*
* @return Return the implementing class's version
*/
public String version();
/**
* Default method example.
*
* @return Return the version of this interface
*/
default String interfaceVersion() {
return "1.0";
}
}
You can then call this method on any implementing class.