When to use: Java 8+ interface default method, vs. abstract method
Java 8 allows for default implementation of methods in interfaces called Default Methods.
I am confused between when would I use that sort of interface default method
, instead of an abstract class
(with abstract method(s)
).
So when should interface with default methods be used and when should an abstract class (with abstract method(s)) be used? Are the abstract classes still useful in that scenario?
Solution 1:
There's a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default
) method in the interface.
The constraint on the default method is that it can be implemented only in the terms of calls to other interface methods, with no reference to a particular implementation's state. So the main use case is higher-level and convenience methods.
The good thing about this new feature is that, where before you were forced to use an abstract class for the convenience methods, thus constraining the implementor to single inheritance, now you can have a really clean design with just the interface and a minimum of implementation effort forced on the programmer.
The original motivation to introduce default
methods to Java 8 was the desire to extend the Collections Framework interfaces with lambda-oriented methods without breaking any existing implementations. Although this is more relevant to the authors of public libraries, you may find the same feature useful in your project as well. You've got one centralized place where to add new convenience and you don't have to rely on how the rest of the type hierarchy looks.
Solution 2:
There are a few technical differences. Abstract classes can still do more in comparison to Java 8 interfaces:
- Abstract class can have a constructor.
- Abstract classes are more structured and can hold a state.
Conceptually, main purpose of defender methods is a backward compatibility after introduction of new features (as lambda-functions) in Java 8.
Solution 3:
This is being described in this article. Think about forEach
of Collections.
List<?> list = …
list.forEach(…);
The forEach isn’t declared by
java.util.List
nor thejava.util.Collection
interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation.The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.
Solution 4:
As described in this article,
Abstract classes versus interfaces in Java 8
After introducing Default Method, it seems that interfaces and abstract classes are same. However, they are still different concept in Java 8.
Abstract class can define constructor. They are more structured and can have a state associated with them. While in contrast, default method can be implemented only in the terms of invoking other interface methods, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.