Narendra Pathai Narendra Pathai - 5 months ago 38x
Java Question

Interface with default methods vs Abstract class in Java 8

As Java 8 allows for default implementation of methods in interface called Default Methods, there seems to be a confusion between when would I use a

abstract class

So when should interface with default methods be used and when should an abstract class be used? Are the abstract classes still useful in that scenario?


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.