Interfaces

  • Interface definitions are much like class definitions in which all (non-default) methods are abstract.

  • Regardless of Java 8’s introduction of default methods that implement the method, Java interfaces are not able to handle state.

  • The motivation for default methods was to make the implementation of the Java Collections framework easier.

Java 8+ Interface Default Method

Reference URL: https://stackoverflow.com/questions/19998454/when-to-use-java-8-interface-default-method-vs-abstract-method

  • The good thing about default methods is that, from the implementation class, you can use the higher-level default method, or have the option to overide the implementation of the method. Before Java 8, there was no choice. All interface methods were abstract and had to be implemented. In this sense, it allows Java classes to have some semblance of multiple inheritence.

The constraint with default method are:

  • it can only be implemented with calls to other interface methods, with no reference to a particular implementation’s state. Remember that the implementation class is the one that uses this interface. Thus, the default method would not know about the implementation details. Hence, the main use case for the default method, is to use higher-level and convenience methods.

  • cannot call a private method of the interface (Java 9 and up)