springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial

Related Posts

  • Java 9 @SafeVarargs Annotation Enhancement Use Java Try with Resources Create Private Methods in Java 9 Java 9 Features with Examples Optional Class in Java BiConsumer Functional Interface in Java What is the BiFunction in Java 8 What is the BiPredicate in Java 8 What is Supplier Interface in Java 8 Consumer Functional Interface Example In Java 8
Core Java

Java 8 feature Functional Interface

Last Updated: 27-04-2022 Springjava

This feature is introduced in Java 8. An Interface which contains a single abstract method and any number of default and static methods. Invoking the lambda expression we need a Functional Interface is compulsory. Java Programming Language is an OOP (Object Oriented Programming) based programming language. But from Java 8 onwards we can use the benefits of OOP along with Functional Programming with the help of Lambda and Functional Interface.
Some of  the Predefined Functional Interfaces are:
    • Runnable —>It contains a  single abstract method i.e., run().
    • Callable —> It contains a single abstract method i.e., call().
    • Comparable —> It contains a single abstract method i.e., compareTo().
    • ActionListener —> It contains a single abstract method i.e., actionPerformed(). 

interface Test {
    public void m1();
    default void m3 {
    }
    public static void m3() {
    }
}

→ It is a  Functional Interface.

interface Test {
    public void m1();
    public void m2();
}

→ It is not a Functional Interface.

interface Test {
    default void m3 {

    }

    public static void m3() {

    }
}

→ It is not a Functional Interface.

@FuctionalInterface
interface Test {
    public void m1();
    default void m3 {

    }

    public static void m3() {

    }
}

→ It is also a Functional Interface and the “@FuctionalInterface” annotation is used for checking if the rules of the Functional Interface are satisfied or not.

@FuctionalInterface
interface Test {
    public void m1();
}

@FuctionalInterface
interface Demo extends Test {
}

→ It is a Functional Interface.
Example:

@FunctionalInterface
interface A{
public void m1();
}

class Demo implements A{

@Override
public void m1() {
System.out.println("Hello Functional Interface without Lambda Expression");
		
	}
}

public class Test {
	
	public static void main(String[] args) {
		A a=new Demo();
		a.m1();
	}
}

Output: 

Hello Functional Interface without Lambda Expression

→ In this above example we have Functional Interface i.e., “A”. Because the “A” interface is having SAM(Single Abstract Method) and is also annotated with the “@FunctionalInterface” annotation. This annotation is optional to use for making a Functional Interface.
→ In this example we are implementing that Functional Interface with an extra class i.e., the “Demo” class. This will extra .class file for this program.
→ In this example the length code of the program is lengthy as compared to the Lambda expression implementing this Functional Interface.
→ In this example is generated the three “.class” files are:
      • A.class
      • Demo.class
      • Test.class
Example:

@FunctionalInterface
interface A{
	public void m1();
}

public class Test {
	
	public static void main(String[] args) {
		//Invoking lambda expression
A a=()->System.out.println("Hello Functional Interface with Lambda Expression");
		a.m1();
	}
	
}

Output: 

Hello Functional Interface with Lambda Expression

→ In the above we created one Functional Interface i.e., “A”. This “A” interface is the functional interface because it contains only one SAM(Single Abstract Method) and also this interface is annotated with the “@FunctionalInterface” annotation. This annotation is optional and declared to make a Functional Interface but an Interface contains a single abstract method to treat as a Functional Interface.
→ In this example we don't need to create an extra class to implement this Functional Interface. We implemented this functional interface through Lambda Expression to enable function programming for this example.
→ Here we can see and analyze how code is concise as compared to a previous example to take an extra class for implementation of this functional interface.
→ In the above example is generated two “.class” files are:
       • A.class
       • Test. class

Conclusion:
This topic is explained What is a Functional Interface? How can we make a Functional Interface? What is the use of a “@FunctionalInterface” annotation in the functional interface? How can we invoke Lambda Expression?

What is a Java Bean Class? Java 8 Features Write Less Lines Of Code Lambda Expression In Java 8 Features

Leave your thought here

Your email address will not be published. Required fields are marked *

springjava_com
  • springjavateam@gmail.com
  • springjava.com
Learn
    • Core Java
    • Spring Core
    • Spring Boot
    • Spring MVC
    • Angular Tutorial
Quick links
  • About Us
  • Contact Us
  • Privacy Policy
Subscribe to Blog via Email
Subscribe
© 2021 Spring Java. All Rights Reserved.
springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial
  • About Us
  • Contact Us