Java Functional Interfaces
An interface having only single abstract method is called functional interface 😊. You can either use the predefined functional interface provided by Java or create your own functional interface and use it.
If you want to check the predefined functional interfaces which is provided by oracle people..go here predefined functional interfaces ,they all have only one abstract method. That is the reason,they are also known as Single Abstract Method interfaces (SAM Interfaces).
Now let me show you how to declare a function interface, below is a code snippet for a functional interface.
To use lambda expression in java,you need to create your own function interface or use predefined functional interface given by java people.
Why need @FunctionaInterface annotation?
While creating your own functional interface, it is good practice to mark it with @FunctionalInterface annotation, this annotation is introduced in Java 8. Although its optional, you should use it so that you get a compilation error if the interface you marked with this annotation is not following the rules of functional interfaces🙋.
In another way you can say that, if an interface annotated with @FunctionalInterface will through compile time error when try to add more then one abstract method in an interface while unannotated interface does not 😇.
Lets see an example of functional interface with annotation 🙋.

So upto this you are in position to tell the thumb rule for Functional Interface 😇😇😇
Rule 1: The functional interface has Only one abstract method. Along with the one abstract method, you can create any number of default and static methods. I will talk about default and static methods in later post.
Now its time to see full example of your own functional Interface..so lets go ahead..😇
If you want to check the predefined functional interfaces which is provided by oracle people..go here predefined functional interfaces ,they all have only one abstract method. That is the reason,they are also known as Single Abstract Method interfaces (SAM Interfaces).
Now let me show you how to declare a function interface, below is a code snippet for a functional interface.
public interface Demo { public abstract void foo(); }Why need functional Interface?
To use lambda expression in java,you need to create your own function interface or use predefined functional interface given by java people.
Why need @FunctionaInterface annotation?
While creating your own functional interface, it is good practice to mark it with @FunctionalInterface annotation, this annotation is introduced in Java 8. Although its optional, you should use it so that you get a compilation error if the interface you marked with this annotation is not following the rules of functional interfaces🙋.
In another way you can say that, if an interface annotated with @FunctionalInterface will through compile time error when try to add more then one abstract method in an interface while unannotated interface does not 😇.
Lets see an example of functional interface with annotation 🙋.
@FunctionalInterface public interface Demo { public abstract void foo(); }If you try to add more the one abstract method, will get compile time error 😊.
So upto this you are in position to tell the thumb rule for Functional Interface 😇😇😇
Rule 1: The functional interface has Only one abstract method. Along with the one abstract method, you can create any number of default and static methods. I will talk about default and static methods in later post.
Now its time to see full example of your own functional Interface..so lets go ahead..😇
@FunctionalInterface public interface Demo { public abstract void foo(); }
class TestClass { public static void main(String[] args) { Demo demo = ()->{System.out.println("Hello from foo method.");}; demo.foo(); } }
OUTPUT : Hello from foo method.How to use predefined functional interface , see with an example. In this example I am going to use java.util.function.IntBinaryOperator , has a method called public abstract int applyAsIn(int a, int b)
import java.util.function.IntBinaryOperator; class TestClass { public static void main(String[] args) { IntBinaryOperator intBinaryOperator = (a,b)->a+b; int sum = intBinaryOperator.applyAsInt(10, 20); System.out.println("Sum of Two Numbers is "+sum); } }
OUTPUT :Sum of Two Numbers is 30That all guys , now I am going to wrap up this session..😊😊..If you have doubt please comment below..I will try to response as soon as possible. Thank youuuu😇😇😇...😇
0 comments:
Post a Comment