Define own functional interfaces java 8
Functional interface
While using lambda expressions we need to implement an interface to execute the lambda expression. That interface is called a functional interface. It has only one abstract method. So that while executing lambda expressions there should be no confusion using which method the expression should be executed.
It is a statement of programmer intent that serves three purposes:
it tells readers of the class and its documentation that the interface was designed to enable lambdas;
it keeps you honest because the interface won’t compile unless it has exactly one abstract method;
it prevents maintainers from accidentally adding abstract methods to the interface as it evolves.
@FunctionalInterface interface Func{ void test(); }
Below are the rules.
Define an interface with one and only one abstract method.
Use @FunctionalInterface annotation in interface definition.
We can define any number of other methods like Default methods, Static methods.
If we override java.lang.Object class’s method as an abstract method, which does not count as an abstract method.
@FunctionalInterface interface FunctionalInterfaceJava { public int incrementByvalue(int a); default void abc() { System.out.println("a"); } static void xyz() { System.out.println("b"); } }
No comments: