Difference between Anonymous Inner Class and Lambda Expression

16 Aug, 2021
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 Interfaceclass MyClass implements Age {    @Override    public void getAge()    {        // printing the age        System.out.print("Age is " + x);    }}
Age is 21
 
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 interfaceslambda 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 methodinterface FuncInterface {    // An abstract function    void abstractFun(int x); // A non-abstract (or default) functiondefault 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);    }}
10
 
Akanksha_Rai
gabaa406
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM
Stack vs Heap Memory Allocation
Difference between Clustered and Non-clustered index
Arrays in Java
Arrays.sort() in Java with examples
Split() String method in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java