Java abstract class with a generic parameter that implements its own generic parameter

I have a difficult problem. I am trying to create a generic callback manager class that uses a generic interface of any type. The plan is to do something like this:

public interface LocationListener {
    void locationChanged();
    void providerChanged();
}

//The implementation declaration below is wrong...
public abstract class CallbackManager<T> implements T {
    //do something
}

public class MyCallbackManager extends CallbackManager<LocationListener> {

   @Override
   public void locationChanged() {

   } 

   @Override
   public void providerChanged() {

   }
}

Generally speaking, I try to avoid creating the MyCallbackManager class as follows:

public class MyCallbackManager extends CallbackManager<LocationListener> implements LocationListener {

   @Override
   public void locationChanged() {

   } 

   @Override
   public void providerChanged() {

   }
}

Is there any way to achieve this? I look forward to your answers.

EDIT

Because you asked to clarify a precedent. This is the idea:

public abstract class CallbackManager<T>  {

    protected interface FunctionalInterface<T> {
        void run(T type);
    }

    protected ArrayList<WeakReference<T>> eventQueue = new ArrayList<>();

    protected void flush(@NonNull FunctionalInterface<T> functionalInterface) {
        for (int i = eventQueue.size() - 1; i >= 0; i--) {
            WeakReference<T> weakReference = eventQueue.get(i);
            T type = weakReference.get();
            if (type == null) {
                unregister(i);
            } else {
                functionalInterface.run(type);
            }
        }
    }
}

public class CallbackManagerLocation extends CallbackManager<LocationListener> implements LocationListener {

   @Override
   public void locationChanged() {
        flush((ll) -> ll.locationChanged());
   } 

   @Override
   public void providerChanged() {
        flush((ll) -> ll.providerChanged());
   }
} 

The embedded interface in CallbackManagerLocation is used only to have exactly the same naming convention as the interface that is held in CallbackManager.

+4
2

, . , , . , , public T locationChanged() CallbackManager. , CallbackManager. .

+2

, ,

public abstract class CallbackManager<T> implements T

, T , , JAVA.

, , , public abstract class CallbackManager<ParentListener> implements ParentListener.

, Cannot refer to the type parameter LocationListener as a supertype ,

+1

Source: https://habr.com/ru/post/1672725/


All Articles