Java 8 - Functional Interface & Annotations
Functional Interface:
* To call the lambda expression, we need functional interface.
* If the interface contain only one abstract method, then it is called functional interface.
* Eg: Comparable contains only one compareTo(), Runnable contains run(), Callable contains only call(), ActionListener (AWT) contains only one actionperformed() etc.,
* Single Abstract Interface (SAM) is also called as Functional Interface
Use of Functional Interface:
Functional interface is used to invoke lambda expression.
Rule for Functional Interface:
* Compulsory Only one abstract method should be there inside the interface. Restrictions is applicable only for abstract methods.
* Mutliple default methods can take.
* Mutliple static methods can take.
Note: If interface declares abstract method overriding one of the public methods of java.lang.Object, that also does not count as a interface abstract method. Since any implementation of the interface will have an implementation from java.lang.object or elsewhere.[emphasis mine]
Eg. equals method . So Comparator is also Functional Interface. Because comparator is having compare and equals method.
Eg. (Functional Interface)
interface Inter{
public void m1(); // Default abstract method. Because inside the interface all methods are abstract methods.
// This is introduced in Java8
default void m2(){
}
// This is introduced in Java8
public static void m3(){
}
}
Eg. (Not Functional Interface) Because 2 abstract methods are there.
interface Inter{
public void m1();
public void m2();
}
Functional interface Annotation:
@FunctionalInterface
Use of Annotation:
By default if programmer violate the rule of funcational interface compiler will give the error.
Note : If you violate the functional interface rule using annotation then exception will throw
"UnExpected @FunctionalInterface ......"
@FunctionalInterface wrt to Inheritance:
Case 1 : Parent interface is a functional interface, And child interface is a empty interface, then child interface also functional interface. (Both @FunctionalInterface)
Case 2 : Parent interface is a functional interface, And child interface is also having same method what parent interface having, then chile interface also funcational interface. (Both @FunctionalInterface)
Case 3 : Parent interface is a functional interface, And child interface is having different method, then child interface is not a functional interface. (So if you give @FunctionalInterface annotation in child interface. It will throw error.)
Case 4 : Parent interface is a functional interface, And child interface is having different method, then child interface is not a functional interface (parent: @FunctionalInterface Chile : No @FunctionalInterface )
* To call the lambda expression, we need functional interface.
* If the interface contain only one abstract method, then it is called functional interface.
* Eg: Comparable contains only one compareTo(), Runnable contains run(), Callable contains only call(), ActionListener (AWT) contains only one actionperformed() etc.,
* Single Abstract Interface (SAM) is also called as Functional Interface
Use of Functional Interface:
Functional interface is used to invoke lambda expression.
Rule for Functional Interface:
* Compulsory Only one abstract method should be there inside the interface. Restrictions is applicable only for abstract methods.
* Mutliple default methods can take.
* Mutliple static methods can take.
Note: If interface declares abstract method overriding one of the public methods of java.lang.Object, that also does not count as a interface abstract method. Since any implementation of the interface will have an implementation from java.lang.object or elsewhere.[emphasis mine]
Eg. equals method . So Comparator is also Functional Interface. Because comparator is having compare and equals method.
Eg. (Functional Interface)
interface Inter{
public void m1(); // Default abstract method. Because inside the interface all methods are abstract methods.
// This is introduced in Java8
default void m2(){
}
// This is introduced in Java8
public static void m3(){
}
}
Eg. (Not Functional Interface) Because 2 abstract methods are there.
interface Inter{
public void m1();
public void m2();
}
Functional interface Annotation:
@FunctionalInterface
Use of Annotation:
By default if programmer violate the rule of funcational interface compiler will give the error.
Note : If you violate the functional interface rule using annotation then exception will throw
"UnExpected @FunctionalInterface ......"
@FunctionalInterface wrt to Inheritance:
Case 1 : Parent interface is a functional interface, And child interface is a empty interface, then child interface also functional interface. (Both @FunctionalInterface)
Case 2 : Parent interface is a functional interface, And child interface is also having same method what parent interface having, then chile interface also funcational interface. (Both @FunctionalInterface)
Case 3 : Parent interface is a functional interface, And child interface is having different method, then child interface is not a functional interface. (So if you give @FunctionalInterface annotation in child interface. It will throw error.)
Case 4 : Parent interface is a functional interface, And child interface is having different method, then child interface is not a functional interface (parent: @FunctionalInterface Chile : No @FunctionalInterface )
Comments
Post a Comment