0

I have seen many question around this, but none seens to be exactly what I am facing and I still don't understand what's wrong. I have a class that implements two interfaces and these two interfaces extends from another interface with generics. Whoever it seens like I can't implement these two interfaces in the same class, as I have an error saying 'cannot be inherited with different type arguments'.

Interface with Generics:

public interface Observer<O> {
    void onEvent(O data);
}

Interface A:

public interface EventAObserver extends Observer<String> {
}

Interface B:

public interface EventBObserver extends Observer<Integer> {
}

Class that needs to listen both events and gets the error:

public class Listener implements EventAObserver, EventBObserver {
    @Override
    public void onEvent(Integer data) {

    }

    @Override
    public void onEvent(String data) {

    }
}

All this trouble because somewhere else in my code I have a list of observers and I want to broadcast events just like:

for (Observer observer : observers)
    observer.onEvent(data);

Is it possible to solve this inheritance problem? Or should I try something entirely diffent?

Thanks!

1 Answers1

0

You should try something different because of type erasure for generics during runtime. This means during runtime the specified types for generics all are replaced by Object. And therefore the onEvent methods in Listener look the same during runtime.

Michael Katt
  • 615
  • 1
  • 5
  • 18