Wednesday, 3 June 2015

Functional interface annotation in Java 8

In the post about functional interface we have already seen that functional interfaces are those interfaces that have only one abstract method.

Java 8 also introduces an annotation @FunctionalInterface to be used with functional interface. Annotating an interface with @FunctionalInterface indicates that an interface type declaration is intended to be a functional interface.

It is not mandatory to mark functional interface with @FunctionalInterface annotation, it is more of a best practice to do that and also gives a surety that no other abstract method will be added accidentally to the functional interface. Because it will result in compiler error if any other abstract method is added to a functional interface which is annotated with @FunctionalInterface annotation.

Let's see it with some examples what is permitted and what is not with FunctionalInterface annotation.
A valid example of a functional interface -

 
@FunctionalInterface
public interface IMyFuncInterface {
  public void getValue();
}

Note that in Java 8 default methods and static methods are also added in interface which means interface can have a method with default implementation and static methods in Java 8. In a functional interface there may be one or more default methods/static methods but there should be only one abstract method. It is ok to have a functional interface like following.

 
@FunctionalInterface
public interface IMyFuncInterface {
    int func(int num1, int num2);
    // default method
    default int getValue(){
        return 0;
    }    
}

When you annotate an interface with @FunctionalInterface, if more than one abstract method is defined it will throw a compiler error.

@FunctionalInterface
public interface IMyFuncInterface {
  public void getValue();
  // Second abstract method so compiler error
  public void setValue();
}

A functional interface can specify Object class public methods too in addition to the abstract method. That interface will still be a valid functional interface. The public Object methods are considered implicit members of a functional interface as they are automatically implemented by an instance of functional interface.

As exp. This is a valid functional interface

@FunctionalInterface
interface IFuncInt {
    int func(int num1, int num2);
    // default method
    default int getValue(){
        return 0;
    }
    public String toString();
    public boolean equals(Object o);
}

That's all for this topic Functional interface annotation in Java 8. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Overview of lambda expressions
  2. functional interfaces & lambda expression
  3. Lambda expression and variable scope
  4. Lambda expression and exception handling
  5. Lambda expression as method parameter
  6. Method reference in Java 8
  7. Lambda expression examples in Java 8

You may also like -

No comments:

Post a Comment