Lambda Expression – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 27 Dec 2020 15:11:29 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Lambda Expression – Programmerbay https://programmerbay.com 32 32 Difference between Anonymous Inner Class and Lambda Expression in Java 8 https://programmerbay.com/difference-between-anonymous-inner-class-and-lambda-expression-in-java-8/ https://programmerbay.com/difference-between-anonymous-inner-class-and-lambda-expression-in-java-8/#respond Fri, 14 Aug 2020 15:49:25 +0000 https://programmerbay.com/?p=6432 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.

Difference between anonymous inner class and Lambda Expression in Tabular Form

BasisAnonymous inner classLambda expression
DefinitionA class without having name termed as anonymous inner classIt is an anonymous function that provides a way to implement functional programming
Abstract MethodsIt can be used with interfaces having any number of abstract methodsIt can be used only with functional interface( having exactly one abstract method )
Instance CreationBeing a class, it can have its own instancesit can't be instantiated
MemoryDuring compilation, a separate .class file is generated that occupies more memory than Lambda expressionNo new file is generated
AdvantageTo avoid unnecessary creation of sub classesTo implement functional programming
this keywordthis keyword always refer to the instance of the anonymous class itself, not enclosing classthis keyword always refer to instance of enclosing class
UsageIt can be used with concrete classes, abstract classes.It cannot be used with concrete classes, abstract classes.

Anonymous inner class [Click here : to read in more depth]

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 !!

Lambda Expression

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

Key Differences:

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

 

]]>
https://programmerbay.com/difference-between-anonymous-inner-class-and-lambda-expression-in-java-8/feed/ 0