Functional Interface: is an interface it contains only one abstract method and declares with annotation @FunctionalInterface
E.g:
@FunctionalInterface
interface ReverseString{
String reverse(String n);
}
public class ImplementingLamdaExpression {
public static void main(String args[]){
ReverseString myreverse= str ->{
String result="";
for(int i=str.length()-1;i>0;i--){
result=result+str.charAt(i);
}
return result;
};
System.out.println(myreverse.reverse("avinash"));
}
}
Why?- To enable functional programming in java we use lamda expression
Advantages of Functional programming:
Runnable-> only one abstract method - run()
Callable-> only one abstract method - call()
Comparable-> only one abstract method -compareTo()
Default and static methods in an interface:
Before 1.8 interface contains only abstract methods. Later, interface allows default static methods
i.e. functional interface can have 100's of default and static methods but should have only one abstract method
Interface interf
{
public void abstract m4();-- This wont throw any error and will not be considered as functional interface as it is having more than on abstract method
public void abstract m1();
public void default m2()
{
}
public void static m3()
{
}
}
@FunctionalInterface(This is the using of @FunctionalInterface annotation)
Interface interf
{
public void abstract m1();
public void abstract m4();-- This will throw error saying that functional interface should have only abstract method
public void default m2()
{
}
public void static m3()
{
}
E.g:
@FunctionalInterface
interface ReverseString{
String reverse(String n);
}
public class ImplementingLamdaExpression {
public static void main(String args[]){
ReverseString myreverse= str ->{
String result="";
for(int i=str.length()-1;i>0;i--){
result=result+str.charAt(i);
}
return result;
};
System.out.println(myreverse.reverse("avinash"));
}
}
Why?- To enable functional programming in java we use lamda expression
Advantages of Functional programming:
- you can assign function to a variable i.e. a=func()
- you can pass function as parameter to other function i.e. fun(fun)
Runnable-> only one abstract method - run()
Callable-> only one abstract method - call()
Comparable-> only one abstract method -compareTo()
Default and static methods in an interface:
Before 1.8 interface contains only abstract methods. Later, interface allows default static methods
i.e. functional interface can have 100's of default and static methods but should have only one abstract method
Interface interf
{
public void abstract m4();-- This wont throw any error and will not be considered as functional interface as it is having more than on abstract method
public void abstract m1();
public void default m2()
{
}
public void static m3()
{
}
}
@FunctionalInterface(This is the using of @FunctionalInterface annotation)
Interface interf
{
public void abstract m1();
public void abstract m4();-- This will throw error saying that functional interface should have only abstract method
public void default m2()
{
}
public void static m3()
{
}
0 comments:
Post a Comment