Java 8: Functional Interface


“An interface having exactly one abstract method is called Function Interface. Along with one abstract method can have any number of default and static methods.”

Points to Remember

  • This is also called as Single Abstract Method Interfaces or SAM Interfaces.
  • The default method having implementation is not considered as an abstract method.
  • An interface declares an abstract method overriding one of the public methods of java.lang.Object is not considered as an abstract method because any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
  • If you declare an interface with @FunctionalInterface then the annotated interface must satisfy the requirements of a functional interface, if not match requirement will get the compile-time issue.

Note: Compiler will treat an interface as a functional interface if fulfilling conditions of Functional interface regardless of whether or not declared with @FunctionalInterface annotation.

Where to use Functional Interface

  • Functional Interface uses to represent Lambda Expressions.

Create your own functional interface

We can create our own Functional Interface by using @FunctionalInterface or Define interface having only one abstract method.

@FunctionalInterface
interface YourFunctionalInterface
{
 public int addValues(int a, int b);
}

Use of Functional Interface

We can use our own functional interface as above or predefined interface also such as Runnable, ActionListener, Comparator, etc. all these having a single abstract method.

Example: Use your own functional interface

In this example, define the functional interface “YourFunctionalInterface” definition by the lambda expression for arguments (a,b). When we call the functional interface method with the argument (120,100)  then it will make reference to the given definition of FunctionalInterface and return the result as 220.

public class CalculateClass {

   public static void main(String args[]) {
        // lambda expression
    YourFunctionalInterface sum = (a, b) -> a + b;
        System.out.println("Result: "+sum.addValues(120, 100));
    }
}

Output


Result: 220

Example: Use a predefined functional interface

In this thread, execution example consider both ways legacy and lambda expression to implement the run method and start threads.

public class PredefinedFunctionalInterfaceExample {
public static void main(String[] args) {

// Implementing Runnable using anonymous class (legacy way)
Runnable runnable1 = new Runnable() {
@Override
public void run() {
System.out.println("Thread name : " + Thread.currentThread().getName());
}
};
Thread thread1 = new Thread(runnable1);

// Implementing Runnable using Lambda expression because Runnable having
// only one abstarct method run()
Runnable runnable2 = () -> {
System.out.println("Thread name : " + Thread.currentThread().getName());
};
Thread thread2 = new Thread(runnable2);

// Start Threads
thread1.start();
thread2.start();
}
}

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s