Java 8 introduces a functional interface to support lambda expressions. Only an interface of an abstract method can be called as a function interface.
Runnable,comparator,coneable are examples of functional interfaces. We can implement these functional interfaces by lambda expressions.
For example:
Thread t =new Thread(new Runnable(){ publicvoidrun(){ System.out.println("Runnable implemented by using Lambda Expression"); }});
This is how the thread was built before Lambda was introduced.
RUNNABL has only one abstract method, and we can think of it as a functional interface. We use lambda expressions like this:
new Thread(()->{ System.out.println("Runnable implemented by using Lambda Expression");});
Here we only pass lambda expressions instead of runnable objects.
Declare our own functional interface
We can define a separate abstract method in an interface to declare our own functional interfaces.
Public Interfacefunctionalinterfacetest{voidDisplay ();}//Implement the test class for the above interface Public classFunctioninterfacetestimpl { Public Static void Main(string[] args) {//old way with anonymous inner classFunctionalinterfacetest fit =NewFunctionalinterfacetest () { Public void Display() {System. out. println ("Display from old"); }}; Fit.display ();//outputs:display from the old //with lambda expressionFunctionalinterfacetest Newway = () {System. out. println ("Display from New Lambda Expression");} Newway.display ();//outputs:display from new Lambda Expression}}
We can add @functionalinterface annotations to show compile-time errors. This optional
For example:
@FunctionalInterfacepublicinterface FunctionalInterfaceTest{ void display(); void anotherDisplay();//报错, FunctionalInterface应该只有一个抽象方法}
Default method
A function interface can have only one abstract method, but there can be more than one default method.
The default method, introduced in Java 8, adds a new method to the interface without affecting the implementation class.
Interface defaultinterfacetest{voidShow ();default voidDisplay () {System.out.println ("Default method from interface can has body..!"); }} Public class Defaultinterfacetestimpl implements defaultinterfacetest{ Public void Show() {System.out.println ("Show Method"); }//We do not need to implement the default method Public Static void Main(string[] args) {Defaultinterfacetest obj =NewDefaultinterfacetestimpl (); Obj.show ();//Output: Show methodObj.display ();//Output: Default method from interface can has body..!}}
The main purpose of the default method is to not enforce the class, we can add a method (non-abstract) to the interface.
Multiple implementations
If the same default method appears in two interfaces, and a class implements both interfaces, an error is thrown.
//Common interface with show () method interface Test{ default voidShow () {System.out.println ("Show from Test"); }}//Another interface with the same show () method interface anothertest{ default voidShow () {System.out.println ("Show from Test"); }}The //main class implements two interfaces class Main implements Test, anothertest{//The show () method here has an inheritance ambiguity}
This class cannot compile because the show () method of the Test,anothertest interface is ambiguous, in order to solve this problem we need to rewrite the show () method inside the main class.
class Main implements Test, AnotherTest{ void show(){ System.out.println("Main show method"); }}
Java 8: Example of a functional interface