Functional Interfaces
In single line description, Interface with only abstract method are referred as Functional Interface. However, A functional interface can have more than one static or default methods, and also can Override some methods from java.lang.ObjectLuckily you do not need to verify the Functional Interface, Compiler will do it for you. But you can specify @FunctionalInterface annotation to reduce it’s workload.
Below is an example of Functional interface.
interface Demo { // Only abstract method. void method1(Collection collection); // Functional interface can have more than one static or default methods. default void method2() { System.out.println("This is default in Functional Interface."); } // And also can override methods of java.lang.Object @Override public String toString(); }
Some frequently used and popular Functional Interfaces are:
- java.awt.event.ActionListener;
- java.lang.Runnable
- java.util.concurrent.Callable
- java.security.PrivilegedAction
- java.util.Comparator
- java.io.FileFilter
- java.beans.PropertyChangeListener
Note :
A new package in Java 8 is added that contains functional interfaces that are commonly used for lambda expression and method reference : java.util.function
No comments:
Post a Comment