An anonymous class is a class without a name, whose initialisation and instantiation are made at the same time. Whereas, Lambda expression is a method without a name, used to provide method behaviour or body of the method of functional interface.
Basis | Anonymous inner class | Lambda expression |
---|---|---|
Definition | A class without having name termed as anonymous inner class | It is an anonymous function that provides a way to implement functional programming |
Abstract Methods | It can be used with interfaces having any number of abstract methods | It can be used only with functional interface( having exactly one abstract method ) |
Instance Creation | Being a class, it can have its own instances | it can't be instantiated |
Memory | During compilation, a separate .class file is generated that occupies more memory than Lambda expression | No new file is generated |
Advantage | To avoid unnecessary creation of sub classes | To implement functional programming |
this keyword | this keyword always refer to the instance of the anonymous class itself, not enclosing class | this keyword always refer to instance of enclosing class |
Usage | It can be used with concrete classes, abstract classes. | It cannot be used with concrete classes, abstract classes. |
A class without having any name is called anonymous inner class. It permits declaring and defining an object attribute and behaviour at the same time.
It concise the code and helps in avoiding the creation of sub classes.
It avoids the creation of subclass just for sake of overriding a method. It can be used with concrete class, abstract class, and interface.
Syntax:
className obj = new className{ //body }
for example:
interface testInterface { void display(); void extra(); } public class AnonymousInnerClass { public static void main(String[] args) { testInterface test = new testInterface() { @Override public void display() { System.out.println("My anonymous class implement testInterface"); } @Override public void extra() { System.out.println("Another abstract method !! "); } }; test.display(); test.extra(); } }
Output:
My anonymous class implement testInterface Another abstract method !!
A lambda expression is an anonymous function that uses functional interface to implement functional programming in Java.
It works in companion with functional interface. A lambda expression can be defined as an anonymous function that represents instance of functional interface.
On the other hand, a functional interface is an interface that consists exactly one abstract method that later, referenced with lambda expression.
@FunctionalInterface annotation is used to tell the compiler that this interface is intended to be functional interface, if any criteria is violated the compiler would trigger compile time error. It is not mandatory to put the annotation, indeed, it is a good practise though.
It concise and reduces the complexity of the code.
For example
interface testInterface { void display(); } public class AnonymousInnerClass { public static void main(String[] args) { testInterface test = () -> System.out.println("My Lambda Expression uses Function interface"); test.display(); } }
Output:
My Lambda Expression uses Function interface
1) Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code
2) Lambda expression cannot be used at all places where anonymous inner class, on the flip side, anonymous inner class can be used wherever Lambda expression is used
3) Inner anonymous class is more powerful as we can use as many methods as we want, whereas Lambda expression can only be used where an interface having only single abstract method
This post was last modified on December 27, 2020