Java Functional Interface: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=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. <syntaxhighlight lang="java"> // Java program to demonstrate functional interface class Test { public static void main(String args[]) { // create anonymous inner class object new Thread(ne..."
 
No edit summary
Line 20: Line 20:
         }).start();
         }).start();
     }
     }
}
</syntaxhighlight>
===Java Example===
And using lambda became
<syntaxhighlight lang="java">
// 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();
  }
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 00:32, 4 July 2025

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();
  }
}