Functional Interfaces:
if an interface contain only one abstract method, such type of interfaces are called functional interfaces and the method is called functional method or single abstract method(SAM).
Example:
1) Runnable : It contains only run() method
2) Comparable : It contains only compareTo() method
3) ActionListener : It contains only actionPerformed()
4) Callable : It contains only call()method
Inside functional interface in addition to single Abstract method(SAM) we can write any number of default and static methods.
In Java 8 , SunMicroSystem introduced @FunctionalInterface annotation to specify that the interface is FunctionalInterface.
InsideFunctionalInterface we can take only one abstract method,if we take more than one abstract method then , we will get compilation error.
Inside FunctionalInterface we have to take exactly only one abstract method.If we are not declaring that abstract method then compiler gives an error message.
FunctionalInterface with respect to Inheritance:
If an interface extends FunctionalInterface and child interface doesn’t contain any abstract method then child interface is also FunctionalInterface
In the child interface we can define exactly same parent interface abstract method.
In the child interface we can’t define any new abstract methods otherwise child interface won’t be FunctionalInterface and if we are trying to use @FunctionalInterface annotation then compiler gives an error message.
if an interface contain only one abstract method, such type of interfaces are called functional interfaces and the method is called functional method or single abstract method(SAM).
Example:
1) Runnable : It contains only run() method
2) Comparable : It contains only compareTo() method
3) ActionListener : It contains only actionPerformed()
4) Callable : It contains only call()method
Inside functional interface in addition to single Abstract method(SAM) we can write any number of default and static methods.
In Java 8 , SunMicroSystem introduced @FunctionalInterface annotation to specify that the interface is FunctionalInterface.
InsideFunctionalInterface we can take only one abstract method,if we take more than one abstract method then , we will get compilation error.
Inside FunctionalInterface we have to take exactly only one abstract method.If we are not declaring that abstract method then compiler gives an error message.
FunctionalInterface with respect to Inheritance:
If an interface extends FunctionalInterface and child interface doesn’t contain any abstract method then child interface is also FunctionalInterface
In the child interface we can define exactly same parent interface abstract method.
In the child interface we can’t define any new abstract methods otherwise child interface won’t be FunctionalInterface and if we are trying to use @FunctionalInterface annotation then compiler gives an error message.
No comments:
Post a Comment