Functional Interface in Java


Q. Why are we learning functional interface?

Ans:- We are learning functional interface because a Lambda Expression is never executed on it’s ownIt is always used with functional interfaces. 
So, before we understand how to use a lambda expression , we must first understand what is a functional interface.


Q. What is a functional interface?

Ans:- The functional interface was introduced in Java 8. functional interface in Java is an interface which has only one abstract method. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of Object class.
They are also called as SAM interfaces. The acronym SAM stands for Single Abstract Method.
SAM Interface is not a new concept.

In Java, we already have many examples of such SAM interfaces like:-

Runnable:- It has only 1 abstract method called run().
ActionListener:- It has only 1 abstract method called actionPerformed().
Comparator:- It has only one abstract method called compareTo().


It’s only that from Java 8 , these interfaces are referred as functional interface. 

Here is an example of a functional interface:-

          public interface Employee{
                     double salary();
          }

In this case, the method salary( ) is implicitly abstract, and it is the only method defined by Employee interface. Thus, Employee is a functional interface, and it's single abstract method is salary( ).

What other members can a functional interface contain?

Ans:- A functional interface can contain any kind of members variables, default methods or static methods etc. but it must have one and only one abstract method.
So the following example is also a functional interface:-

    This is because the interface MyInterface has default and static methods but it still has a SAM called test().

The @FunctionlInterface Annotation:- 

Java 8 introduces an annotation called @FunctionalIterface.
This annotation can be used for compiler level errors. When the interface we have annotated violates  the contract of exactly one abstract method. 

To understand this, look at the following functional   interface:-

          @FunctionalInterface
             public interface MyInterface
             {
                     public void firstWork();
             }

This compiles perfectly fine because our code is complying with SAM rule in the interface MyInterface.

Now, Let us consider the following functional interface:-

          @FunctionalInterface
           public interface MyInterface
           {
                  public void firstWork();
                  public void doSomeMoreWork();
           }


Above will result into compiler error as given below:-

Unexpected @FunctionalInterface annotation @FunctionalInterface ^ Mylnterface is not  a functional interface multiple non-overriding abstract methods found in interface MyInterface.

Important points about functional interface:-
1) As we have discussed, only one abstract method is allowed in any functional interfaceSecond abstract method is not permitted in a functional interface.

2) If we remove @FunctionalInterface annotation then we are allowed to add another abstract method, but it will make the interface non-functional interface.


     3) The use of @FunctionalInterface annotation is optional.

     4)  functional interface is valid even if the @FunctionalInterface annotation is omitted. It is only for informing the compiler to enforce  single abstract method inside interface.

    So the following 2 are exactly same:- 
   
@FunctionalInterface              
    public interface Greetings {
               String greet( );
    }
          OR
public interface Greetings {
            String greet( );
}

5)  Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. Since default methods are not abstract we’re free to add as many default methods to our functional interface as we like.

6)  If an interface declares an abstract method overriding one of the public methods of java.lang.Objectthat also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

7) The package java.util.function  contains many builtin functional interfaces in Java 8. Some of the useful java 8 functional interfaces are Predicate, Function, Supplier, Consumer.

Post a Comment

0 Comments