How to prevent an accidental damage to your Java 8 Functional Interfaces

Spread the love
  •  
  •  
  •  
  •  

Single Abstract Method Interfaces serve as a fundamental building block for functional programming within Java 8. The design of Lambda expressions in Java 8 is fundamentally dependent on Single Abstract Method Interfaces.

And as we all know,  any Java interface can be classified as a Single Abstract Method Interface (or Functional Interface) as long as there is only one single abstract method in the interface. There may be more non-abstract methods, but there should just one and only one abstract method.

Now, let us imagine the following case.

You create a Java interface that has one abstract method. Lets us say its named as DataProcessor and has one method process, that takes a generic argument and returns a generic argument.

From a usage perspective,

  1. You are using it as any other Java interface and have concrete classes implementing this interface in your code base.
  2. Some third party client libraries are using the same interface through Lambda expressions. Obviously, the third party client code base is not in your control, and since Java 8 allows using any Single Abstract Method Interfaces to be used through Lambda expressions, it is perfectly fine for anyone to use that interface as functional interfaces.

And one fine day, a new programmer joins your team, and inadvertently adds another abstract method to that interface without really knowing about the third party dependencies. So the new team member adds a new abstract method, implements the new method in your respective concrete implementation classes and happily commits the code, thinking that all is well.

And then, reality strikes…obviously, the third party client code breaks. Because that code is dependent on the fact that your interface just contained one Single Abstract Method, which is not true anymore.

To avoid this kind of an awkward situation, Java 8 provides an annotation named @FunctionalInterface. Although its use is optional, this annotation is used to annotate interfaces that are meant to be used as Functional Interfaces, and thus, it can be used only for interfaces that have one Single Abstract Method.

So, in the above scenario, if our interface was originally annotated with @FunctionalInterface, the Java compiler would not have allowed adding a new abstract method to the interface – it would have complained right at the point when someone tried to add a new abstract method to it, thus preventing the subsequent problems in the third party client code.

To summarize, use of annotation @FunctionalInterface for single abstract method interfaces is not mandatory, however it is always advisable to use this annotation for interfaces that are meant to be used as Single Abstract Method Interfaces.

As example of the above interface with use of annotation is given below:

Happy coding, Happy sharing!

Cheers

Amit


Spread the love
  •  
  •  
  •  
  •  

2 Comments

  1. Thank you very much for sharing such valuable and informative blog post about how to learn java programming language. I have read many blogs but here i found something new so great explanation keep posting.

Leave a Reply

Your email address will not be published. Required fields are marked *