Java Functional Interface

From bibbleWiki
Revision as of 00:32, 4 July 2025 by Iwiseman (talk | contribs)
Jump to navigation Jump to search

Functional Interface

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit.

Java Example

Before Java 8, we had to create anonymous inner class objects or implement these interfaces.

 
// Java program to demonstrate functional interface
  
class Test
{
    public static void main(String args[])
    {
        // create anonymous inner class object
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                System.out.println("New thread created");
            }
        }).start();
    }
}

Java Example

And using lambda became

 
// Java program to demonstrate Implementation of
// functional interface using lambda expressions
  
class Test
{
  public static void main(String args[])
  {
  
    // lambda expression to create the object
    new Thread(()->
       {System.out.println("New thread created");}).start();
  }
}