Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is adding default methods to interfaces in Java 8 a good design choice and what are the alternatives [duplicate]

Tags:

I am just learning Java, so it is hard for me to access the possible alternatives, and the impact of such a design decision.

Java 8 adds the default methods feature to interfaces, which allows interfaces to have an implementation. This allows to extend the existing interfaces with new methods, without breaking the clients, evolving the interface over time in a backward-compatible way. However, given default implementation, such extensions are somewhat limited, and are likely to be implemented using existing interface methods of the interface or library methods. So my question is

  • Why was this language feature introduced?
  • What key new features does it support? (for instance Splititerators)
  • What other alternatives were there to support those language features? For example, why not create a new interface SplitIterable that extends Iterable?
  • What would be the impact of implementing those alternatives (poliferation of interfaces?)
  • Should I provide a default implementation for a method in the first edition of interface when it is possible to implement it as a composition of other methods?
like image 773
ironstone13 Avatar asked Mar 31 '16 14:03

ironstone13


People also ask

Why do we need default methods 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.

What is the use of default methods in Java 8?

Default methods enable you to add new functionality to existing interfaces and ensure binary compatibility with code written for older versions of those interfaces. In particular, default methods enable you to add methods that accept lambda expressions as parameters to existing interfaces.

What is the use of default and static methods in interface Java 8?

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. A static method is a method that is associated with the class in which it is defined rather than with any object.

What is the advantage of interface in Java 8?

The most obvious benefit of using a Java 8 interface is its new concrete method capability. Another welcome benefit of Java 8 Interfaces is the ability to add new concrete methods to an existing interface, which has already been implemented, without breaking the program.


1 Answers

Why was this language feature introduced?

It was added primarily to let you add methods to existing interfaces that are already in use without breaking everyone's code, but also to share method implementations "horizontally" across classes implementing the same interface (as opposed to "vertical" sharing through inheritance).

What key new features does it support? (for instance Splititerators)

java.util.Collection<T>.stream()

What other alternatives were there to support those language features? For example, why not create a new interface SplitIterable that extends Iterable?

You could opt for entirely new interfaces, and continue with the associated static helper class (e.g. Collection<T> interface and its Collections helper class). This wouldn't be terrible - in fact, one could argue that default methods are purely a syntactic sugar on top of static methods*. However, default methods generally provide for better readability.

What would be the impact of implementing those alternatives (proliferation of interfaces?)

You would end up with a less consistent library, and a less readable language, but it wouldn't be the end of the world. A bigger concern, as pointed out by Joachim Sauer, is that interface implementations would have no way to override implementation from the static helper class. That would take away flexibility.

Should I provide a default implementation for a method in the first edition of interface when it is possible to implement it as a composition of other methods?

You should do it only if you need to share implementation "horizontally". If a method provides essential behavior of the implementation, do not provide a default for it.

* This would be an oversimplification, because default methods remain virtual. Thanks Brian Goetz for the comment.

like image 141
Sergey Kalinichenko Avatar answered Oct 07 '22 08:10

Sergey Kalinichenko