Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 204
Narendra Pathai Avatar asked Nov 15 '13 10:11

Narendra Pathai


People also ask

When should I use interface and abstract class in Java 8?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

What is the difference between an interface with default method and an abstract class?

An abstract class can override Object class methods, but an interface can't. An abstract class can declare instance variables, with all possible access modifiers, and they can be accessed in child classes. An interface can only have public, static, and final variables and can't have any instance variables.

When would you use an abstract class instead of an interface Java?

Abstract classes should be used for (partial) implementation. They can be a mean to restrain the way API contracts should be implemented. In Java 8 for interface #8, you can have default and static methods too.

Why do we need default method in Java 8 interfaces?

Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.


1 Answers

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.

like image 116
Marko Topolnik Avatar answered Nov 08 '22 15:11

Marko Topolnik