Java 7 : Add "public defender methods" to Java interfaces

At this time, we aren't sure that the closures will be included in the Java 7 release. But these doubts have generated a new project : The "public defender methods" proposal.

This new proposal for Java 7 wants to improve the interfaces allowing to add new methods to existing interfaces. The classes implementing the interfaces doesn't need implements these methods. The implementation of these methods are provided using static methods. This could be called virtual extension method.

To illustrate the problem the proposal want to solve, let's take the example of reversing a List. When you have a List and you want to reverse it, you have to use the Collections.reverse() method :

List strings = new ArrayList();

But there is some problems with that code. Because it's a static method, there is no way to override it. We could imagine data structures where the reverse must be made using special algorithms to be efficient. An other problem is that the reverse() method is not in the List interface, so must learn two classes to make a simple thing as reversing a list.

The "public defender methods" give an other way to do that extending the List interface :

public interface List extends Collection {
  extension void reverse() default Collections.reverse;

This add a new method to the List with a default implement that use the Collections.reverse(List list) static method. The list will be passed as the first argument of the static method. So, now that you have a method reverse on the List, you can do that :

List strings = new ArrayList();

This code is a lot better than the other, isn't it ?

You can now override the reverse method providing an implementation specific to your class. But the implementation is now optional. That solve all the problems we see earlier.

To solve multiple inheritance issue a class implementing two interfaces providing a default implementation for the same method name and signature must provide an implementation of the method.

An other objective of this proposal is to "closur-ize" the Java 7 librairies. By example, we could think of a filter() method to the List interface :

public interface List extends Collection {

  extension void filter(Predicate predicate) default Collections.filter;

And a predicate like that :

public interface Predicate {
   boolean accept(E object);

That type of method can take advantage of closures. We can also imagine reduce, forEach, expand, ....

At this time, this is only a proposal, so we aren't sure it'll be included in Java 7 and the syntax is not definitive.

Personally, i think this is a great improvement and that will make the closures (if we have them a day) more interesting for the Java language.

If you want more information on the implementation, you could read the proposal in PDF.

Related articles

  • State of the Lambda
  • Java 7 : More dynamics
  • Java 7 : Oracle pushes a first version of closures
  • Java 7 : Languages updates from Project Coin
  • Java 7 has been released!
  • Java 7 : The new java.util.Objects class
  • Comments

    Comments powered by Disqus