If an interface contain only one abstract method,such type of interface are called Functional Interface and the method is called Functional Method or Single Abstract Method.
Inside functional interface in addition to single Abstract method, we write any number of default and static methods.
In Java8 ,Sun micro system(now Oracle) introduced @FuntionalInterface annotation to specify that the interface is FunctionalInterface.
Inside @FuntionalInterface -we can take only one abstract method. If we take more than one abstract method then compiler raise an error message in compilation.
Inside @FuntionalInterface 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
In 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’t define any new abstract method otherwise child interface won’t be @functionalInterface and if we are trying to use @FuntionalInterface annotation than compiler gives an error message.
In the above example in both parent & child interface, we can write any number of default methods and there are no restrictions. Restrictions are applicable only for abstract methods.
Functional Interface Vs Lambda Expressions
Once we write Lambda expressions to invoke its functionality, then Functional Interface is required. We can use Functional Interface reference to refer Lambda Expression.
Where ever Functional interface concept is applicable there we can use Lambda Expressions.
Anonymous Inner Classes vs Lambda Expressions
Wherever we are using anonymous inner classes there maybe a chance of using Lambda expression to reduce length of the code and to resolve complexity.
- What are the advantages of Lambda expression?
- We can reduce length of the code so that readability of the code will be improved.
- We can resolve complexity of anonymous inner classes. We can provide Lambda expression in the place of object.
- We can pass lambda expression as argument to methods.
- Anonymous inner class can extend concrete class, can extend abstract class,can implement interface with any number of methods but Lambda expression can implement an interface with only single abstract method(Functional Interface).
- Hence if anonymous inner class implements functional interface in that particular case only we can replace with lambda expressions.hence wherever anonymous inner class concept is there,it may not possible to replace with Lambda expressions.
- Anonymous inner class are not equal to Lambda Expression.
- Inside anonymous inner class we can declare instance variables.
- Inside anonymous inner class ‘this” always refers current inner class object(anonymous inner class) but not related outer class object.
- Inside lambda expression we can’t declare instance variables.
- Whatever the variables declare inside lambda expression are simply acts as local variables.
- Within lambda expression ‘this” keyword represents current outer class object reference (that is current enclosing class reference in which we declare lambda expression).
- From lambda expression we can access enclosing class variables and enclosing method variables directly.
- The local variables referenced from lambda expression are implicitly final and hence we can’t perform re-assignment for those local variables otherwise we get compile time error.