Difference between Anonymous Inner Class and Lambda Expression
Anonymous Inner Class:
It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class.
Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.
Anonymous inner class are mainly created in two ways:
- Class (may be abstract or concrete)
- Interface
Syntax: The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.
// Test can be interface, abstract/concrete class
Test t = new Test()
{
// data members and methods
public void test_method()
{
........
........
}
};
To understand the anonymous inner class, let us take a simple program
Java
interface Age {
int x = 21 ;
void getAge();
}
class AnonymousDemo {
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.getAge();
}
}
class MyClass implements Age {
@Override
public void getAge()
{
System.out.print( "Age is " + x);
}
}
|
Lambda Expressions:
Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces
lambda expressions are added in Java 8 and provide below functionalities.
- Enable to treat functionality as a method argument, or code as data.
- A function that can be created without belonging to any class.
- A lambda expression can be passed around as if it was an object and executed on demand.
Java
interface FuncInterface {
void abstractFun( int x);
default void
normalFun()
{
System.out.println( "Hello" );
}
}
class Test {
public static void main(String args[])
{
FuncInterface fobj = ( int x) -> System.out.println( 2 * x);
fobj.abstractFun( 5 );
}
}
|
Table of difference:
Anonymous Inner Class |
Lambda Expression |
It is a class without name. |
It is a method without name.(anonymous function) |
It can extend abstract and concrete class. |
It can’t extend abstract and concrete class. |
It can implement an interface that contains any number of abstract methods. |
It can implement an interface which contains a single abstract methods. |
Inside this we can declare instance variables. |
It does not allow declaration of instance variables, whether the variables declared simply act as local variables. |
Anonymous inner class can be instantiated. |
Lambda Expression can’t be instantiated. |
Inside Anonymous inner class, “this” always refers to current anonymous inner class object but not to outer object. |
Inside Lambda Expression, “this” always refers to current outer class object that is, enclosing class object. |
It is the best choice if we want to handle multiple methods. |
It is the best choice if we want to handle interface. |
At the time of compilation, a separate .class file will be generated. |
At the time of compilation, no separate .class file will be generated. It simply convert it into private method outer class. |
Memory allocation is on demand, whenever we are creating an object. |
It resides in a permanent memory of JVM. |
Last Updated :
21 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...