Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of Default or Defender methods in Java 8

Java 8 has included a new feature called Defender methods which allows creation of default method implementation in interface.

Now first of all this is a huge paradigm shift for all condensed programmers in Java. I viewed a JavaOne 13 presentation given by Brian Goetz where he was discussing about the new stream() and parallelStream() implementations in Collections library.

For adding new methods in Collection interface, they could not have just added a new method without breaking the previous versions. So he told that for catering this a new feature of Default methods was added.

public interface SimpleInterface {   public void doSomeWork();       //A default method in the interface created using "default" keyword   default public void doSomeOtherWork(){     System.out.println("DoSomeOtherWork implementation in the interface");   } } 

Now my question is basically that are default methods just helpful when needed to add new methods to interface without breaking client code? Or are there some other uses to it too?

like image 313
Narendra Pathai Avatar asked Nov 15 '13 09:11

Narendra Pathai


People also ask

What is the purpose of default methods in Java?

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.

Why do we need default methods in Java 8?

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 purpose of default and static method in 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 defender method in Java?

The default methods in an interface in Java are also known as defender methods or, virtual methods. The defender/virtual methods are those that will have a default implementation in an interface.


1 Answers

Besides having the possibility of adding methods to the interface in future versions, there is the important point of allowing an interface to stay a functional interface even if it has more than one method.

A functional interface has only one non-default abstract method which can be implemented via a lambda expression. One example is the Predicate interface which has only one abstract method (test) while providing default methods for negating a Predicate or combining it with another Predicate. Without default methods these methods had to be provided in another utility class like the pre-Java 8 Collections class (as you don’t want to give up the possibility of lambda implementations for such an interface).

like image 74
Holger Avatar answered Sep 29 '22 08:09

Holger