Anudeep Kumar
Anudeep Kumar

Reputation: 1

Does Lambda completely demolish the usage the anonymous inner class from java 8?

I saw many examples how to convert anonymous inner classes to simple lambda expressions .

i understand technical differences between both of them .

But i just want to know when to when ?

what is the business usecase scenario to use Anonymous Inner class after introduction of lambdas ?

Does Lambda completely demolish the usage the anonymous inner class from java 8 ?

Upvotes: 0

Views: 51

Answers (2)

smac89
smac89

Reputation: 43128

Lambdas can only be used in place of SAM (Single Abstract Method) classes. Sometimes these abstract classes are annotated with @FunctionalInterface to emphasise the fact that they can be replaced by a lambda expression.

The answer to your question is No. Anonymous classes still have their usage, the lambda syntax is just there to simplify the creation of classes that only contain a single abstract method such as Function, Callable, etc,

Upvotes: 3

Angel Koh
Angel Koh

Reputation: 13515

imagine this case

interface Anon {
   void m1();
   void m2();
}

and a method that takes this interface

void useAnon(Anon a);

you will not be able to use lambda for this case.

useAnon( new Anon(){
   @Override void m1(){System.out.println("use m1");};
   @Override void m2(){System.out.println("use m2");};

});

Upvotes: 3

Related Questions