-6

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Looks like I am on Java version 10, not 11 – Alexander Mills Jan 31 '19 at 04:36
  • 2
    Possible duplicate of https://stackoverflow.com/questions/47874325/java-8-lambda-expression-with-an-abstract-class-having-only-one-method – Hulk Jan 31 '19 at 04:54
  • 1
    See [Abstract class as functional interface](https://stackoverflow.com/questions/24610207/), [Lambda Expressions for Abstract Classes](https://stackoverflow.com/questions/34424410), [Java 8 lambda expression with an abstract class having only one method (duplicate)](https://stackoverflow.com/questions/47874325/) and [Why do I need a functional Interface to work with lambdas?](https://stackoverflow.com/questions/33010594). – Slaw Jan 31 '19 at 04:55

2 Answers2

3

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.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
  • 1
    @AlexanderMills , your class will always have a name and type..so you cannot use it. – Vishwa Ratna Jan 31 '19 at 04:47
  • 1
    https://stackoverflow.com/questions/47874325/java-8-lambda-expression-with-an-abstract-class-having-only-one-method @AlexanderMills check this link. – Vishwa Ratna Jan 31 '19 at 04:53
  • 2
    More importantly, the objects created for lambda expressions have an unspecified identity, so having a mutable boolean field whose behavior would depend on the object identity is precisely what the Java language wants to prevent. – Holger Jan 31 '19 at 11:25
-1

I solved this problem by keeping the interface but also using a class:

  public static interface AsyncTask<T, E> {
    public void run(AsyncCallback<T, E> cb);
  }

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

  public static abstract class AsyncCallback<T, E> implements IAsyncCallback<T, E> {
    public boolean shortcircuit = false;
  }

in circumstances where I must use an interface, I use the IAsyncCallback interface, where I can use a class, I use AsyncCallback class.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817