Search code examples
javajava-10

How to use class instead of interface for lambda param


Right now I have this:

  public static interface AsyncCallback<T, E> {
    public void done(E e, T v);
  }

I want to convert it to this so I use a boolean property on it:

  public abstract static class AsyncCallback<T, E> {
    boolean shortcircuit = false;
    public abstract void done(E e, T v);
  }

but now I am getting errors:

Inconvertible types; cannot cast '' to 'E'

and

no instance(s) of type variable(s) T exist so that List conforms to AsyncTask

the code I have that generates the errors is based off passing lambdas. Does anyone know why converting the interface to a class causes problems? Even if I comment out the shortcircuit field and just have the done method definition, same errors arise.


Solution

  • You cannot use class instead of Interface ( Precisely to say Fuctional Interface).

    See the link to know why do we need functional Interface to work with lambda in java.

    In your case you are trying to use a class , which will obviously have name, isn't. but to use lambda we have to use anonymous functions which don't have name and type.