Open In App

Difference between Anonymous Inner Class and Lambda Expression

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




// Java program to demonstrate
// the need for Anonymous Inner class
 
interface Age {
    int x = 21;
    void getAge();
}
 
class AnonymousDemo {
    public static void main(String[] args)
    {
        // Myclass is implementation class of Age interface
        MyClass obj = new MyClass();
 
        // calling getage() method implemented at Myclass
        obj.getAge();
    }
}
 
// Myclass implement the methods of Age Interface
class MyClass implements Age {
    @Override
    public void getAge()
    {
        // printing the age
        System.out.print("Age is " + x);
    }
}


Output: 

Age is 21

 

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




// Java program to demonstrate lambda expressions
// to implement a user defined functional interface.
 
// A sample functional interface (An interface with
// single abstract method
interface FuncInterface {
    // An abstract function
    void abstractFun(int x);
 
// A non-abstract (or default) function
default void
    normalFun()
    {
        System.out.println("Hello");
    }
}
 
class Test {
    public static void main(String args[])
    {
        // lambda expression to implement above
        // functional interface. This interface
        // by default implements abstractFun()
        FuncInterface fobj = (int x) -> System.out.println(2 * x);
 
        // This calls above lambda expression and prints 10.
        fobj.abstractFun(5);
    }
}


Output: 

10

 

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
Previous
Next
Share your thoughts in the comments
Similar Reads