Site icon Learn Automation

Functional Interface in Java 8

Functional Interface

In this tutorial, we will discuss about Functional Interface which is the feature in Java 8.

What is Functional Interface?

An interface that contains only one abstract method is called Functional Interface.

SAM – This means Single Abstract Method. It is alias of Functional Interface.

In earlier version we have some functional interface like Comparable, Comparator, Runnable and Callable.

Why functional interface introduced in Java 8?

This is mainly used for enabling the functional programming in Java language. If we want to develop functional programming then we should knowledge of functional interface and then we should knowledge of Lambda expression.

Functional interface is also used for defining Lambda expression to pass a function directly as argument to method.

Functional interface is the pre-requisite of Lambda expression. (It is a base type of Lambda expression).

Syntax to create a functional interface-

Like normal interface, a functional interface is also created by using the keyword.

interface.

    interface <interface name>{

   one abstract method

}

Example-

Interface Addition(){
    Public abstract void add(int a , int b);
}

Sample program of Functional Interface-

@FunctionalInterface  
interface Automation{  
    void selenium(String msg);  
}  
public class FunctionalInterfaceExample1 implements Automation{  
    public void selenium(String msg){  
        System.out.println(msg);  
    }  
    public static void main(String[] args) {  
        FunctionalInterfaceExample1 fie = new FunctionalInterfaceExample1();  
        fie.selenium("Hello Selenium");  
    }  
} 

Output:  Hello Selenium

 

Compiler thinking w.r.t Functional Interface:

If an interface has only one abstract method, compiler considers it as functional interface. Then compiler allows us to create Lambda expression from this interface.

If an interface has zero or more than one abstract method, compiler does not consider it as functional interface. Then complier does not allow us to create lambda expression from those interfaces.

Note:  A functional interface can extends another interface when it does not have any abstract method.

interface automation{  
    void selenium(String msg);   // abstract method  
}  
@FunctionalInterface  
interface automation1 extends automation{  
    // Invalid '@FunctionalInterface' annotation; Doable is not a functional interface  
    void selenium1();  
}  

Output: Compile-time error


Pre-defined Functional interfaces?

Before Java 8 version

 

list of functional interfaces in java 8–

The package java.util.function has 43 functional interfaces.

etc..

You can find more details in below link-

Java 8 Features

I will explain few important functional interfaces which is in Java 8 onwards features.

Predicate: It has an abstract method test which returns a Boolean value as a result.

Syntax-

public interface Predicate
{
   public boolean test(T  t);
 }

BinaryOperator: It has an abstract method apply which takes two arguments and returns a result of the same type.

Syntax-

public interface BinaryOperator 
{
     public T apply(T x, T y);
}       

Function: It has an abstract method apply which takes argument of type T and returns a result of Type R.

Syntax-

public interface Function 
{
   public R apply(T t);
}

Example:

package Practice;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class Test1 {
    
    public static void main(String[] args) {
        
        
        List<String> names = new ArrayList<String>();
        
        names.add("Java");
        names.add("Ruby");
        names.add("Selenium");
        names.add("Python");
        names.add("Java2");
        
        
        // Lambda expression to create object
        Predicate<String> p = (s)-> s.startsWith("J");
        
        // Iterate using for-each loop
        for(String st : names) {
            
            if(p.test(st)) {
                
                System.out.println(st);
                
            }
            
        }
        
        
        
        
    }

}

 

Hope this tutorial is helpful for you to learn Functional Interface. Happy Learning 🙂

 

Exit mobile version