Functional Interface

 A functional interface is an interface that has exactly only one abstract method(SAM).

Example:- Sample example without and with a functional interface and lambda interface.

@FunctionalInterface

public interface Bike {

public void pulsar();

}

public class BikeImpl implements Bike{

@Override

public void pulsar() {

System.out.println("This is pulsar method implementation");

}

}

public class FunInterfaceTest {

public static void main(String[] args) {

// Without a functional interface and lambda

BikeImpl bike = new BikeImpl();

bike.pulsar();

}

}

With a Functional interface and lambda expression, No need for BikeImpl.java class just need the Bike.java functional interface and the main method to call it.

public class FunInterfaceTest {

 public static void main(String[] args) {

//Using the functional interface and lambda

Bike bikeFn = () -> System.out.println("This is functional interface call using lambda expression");

bikeFn.pulsar();

}

}

Post a Comment (0)
Previous Post Next Post