Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda Expressions for Abstract Classes

Tags:

java

lambda

I have an abstract class with one abstract method. How can I use lambda expressions to instantiate it. It cannot be made into an interface because it extends a class.

public class Concrete<T> {     // Has a bunch of predefined methods. }   public abstract class Abstract<T> extends Concrete<T> {     public T getSomething();     // Uses inherited methods from Concrete class }  public class Driver {     public static void main(String[] args) {         System.out.println(new Abstract<String>() {             public String getSomething() {                 // Returns something using inherited methods from Abstract                               // Class and Concrete Class             }         });     } } 
like image 497
Halbort Avatar asked Dec 22 '15 21:12

Halbort


People also ask

Can we write lambda expression for abstract class?

Finally, an abstract class can't refer to a lambda expression, while the interface can have a single abstract method that can refer to a lambda expression.

Can we define a lambda expression for abstract class with just one abstract method True False?

This is possible because the interface has only one abstract method so the compiler can figure out the name. This can be shortened to: Notice that here are no curly brackets and no return keyword. This is possible because the method returns a boolean and the expression d.

Can we use lambda expression with class?

Lambda expressions are very powerful and can replace many usages of anonymous class but not all. An anonymous class can be used to create a subclass of an abstract class, a concrete class, and can be used to implement an interface in Java. It can even add state fields.

Which is better lambda expression or anonymous inner class?

Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code. An inner anonymous class is more powerful as we can use many methods as we want, whereas lambda expression can only be used where an interface has only a single abstract method.


1 Answers

You cannot directly make a lambda expression target an abstract class, as Sleiman Jneidi pointed out in his answer. However, you can use a workaround:

public class AbstractLambda<T> extends Abstract<T> {     private final Supplier<? extends T> supplier;     public AbstractLambda(Supplier<? extends T> supplier)     {         this.supplier = supplier;     }      @Override     public T getSomething()     {         return this.supplier.get();     } } 

This can be used with a lambda expression:

Abstract<String> a = new AbstractLambda<>(() -> "Hello World"); System.out.println(a.getSomething()); // prints 'Hello World' 

In case your getSomething(...) method has arguments, use a java.util.function.Function or the appropriate interface from the java.util.function package instead of java.util.function.Supplier.


This is also how the java.lang.Thread lets you use a Runnable lambda instead of having to subclass the class:

Thread t = new Thread(() -> System.out.println("Hello World")); t.start(); 
like image 196
Clashsoft Avatar answered Oct 12 '22 23:10

Clashsoft