When use and when not to use default methods in Java interfaces
Java 8 supplies a lot new useful features, unfortunately also new misunderstandings and antipatterns are observed every day. One of that mistake is a wrong use of default method in interfaces.
I noticed that programmers implement default methods with an implementation valid only for some production subclasses or even for non of them (a default implementation is used only in tests). For example, a default method returns empty string or empty list and all production subclasses override it.
The general rule of the use default methods in interface is similar to the principle governing the use of implemented method in abstract class. Default implementation should be a proper for all subtypes, for example:
-
Since Java 8 List class has a
sort
method. Its implementation is valid for all subtypes, but subtypes likeSingletonList
orSynchronizedList
override this implementation. -
If you create an interface
Car
with methodgetName
it is OK to create default implementation which return string"Car"
. This implementation is valid forFord
class which implement interfaceCar
. However,getName
method may by override to return string contains car model like ex."Ford " + model
.
Default methods are powerful tools if they used with lambda expressions and functional interfaces. They are also very useful when you would like to add methods to interface in a lib code and preserve backward compatibility. However, default methods should never provide an invalid behavior.