Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement the same interface multiple times, but with different generics? [duplicate]

I have the following interface, which I want to implement multiple times in my classes:

public interface EventListener<T extends Event> {     public void onEvent(T event); } 

Now, I want to be able to implement this interface in the following way:

class Foo implements EventListener<LoginEvent>, EventListener<LogoutEvent> {      @Override     public void onEvent(LoginEvent event)     {      }      @Override     public void onEvent(LogoutEvent event)     {      } } 

However, this gives me the error: Duplicate class com.foo.EventListener on the line:

class Foo implements EventListener<LoginEvent>, EventListener<LogoutEvent> 

Is it possible to implement the interface twice with different generics? If not, what's the next closest thing I can do to achieve what I'm trying to do here?

like image 868
Ali Avatar asked Mar 03 '14 04:03

Ali


People also ask

Can a class implement different instantiations of the same generic interface?

It is prohibited that a type implements or extends two different instantiations of the same interface. This is because the bridge method generation process cannot handle this situation.

Can we implement COM interface twice?

This is not possible. But for that you could create two different classes that implement EventListener interface with two different arguments.

How many times can you implement the same interface?

A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.

Can an interface have multiple implementations?

No, its an error If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.


2 Answers

Is it possible to implement the interface twice with different generics

Unfortunately no. The reason you can't implement the same interface twice is because of type erasure. The compiler will handle type parameters, and a runtime EventListener<X> is just a EventListener


If not, what's the next closest thing I can do to achieve what I'm trying to do here?

Type erasure can work in our favor. Once you know that EventListener<X> and EventListener<Y> are just raw EventListener at run-time, it is easier than you think to write an EventListener that can deal with different kinds of Events. Bellow is a solution that passes the IS-A test for EventListener and correctly handles both Login and Logout events by means of simple delegation:

@SuppressWarnings("rawtypes") public class Foo implements EventListener {      // Map delegation, but could be anything really     private final Map<Class<? extends Event>, EventListener> listeners;      // Concrete Listener for Login - could be anonymous     private class LoginListener implements EventListener<LoginEvent> {         public void onEvent(LoginEvent event) {             System.out.println("Login");         }     }      // Concrete Listener for Logout - could be anonymous             private class LogoutListener implements EventListener<LogoutEvent> {         public void onEvent(LogoutEvent event) {             System.out.println("Logout");         }     }      public Foo() {         @SuppressWarnings("rawtypes")         Map<Class<? extends Event>, EventListener> temp  = new HashMap<>();         // LoginEvents will be routed to LoginListener         temp.put(LoginEvent.class, new LoginListener());         // LogoutEvents will be routed to LoginListener         temp.put(LogoutEvent.class, new LogoutListener());         listeners = Collections.unmodifiableMap(temp);     }      @SuppressWarnings("unchecked")     @Override     public void onEvent(Event event) {         // Maps make it easy to delegate, but again, this could be anything         if (listeners.containsKey(event.getClass())) {             listeners.get(event.getClass()).onEvent(event);         } else {             /* Screams if a unsupported event gets passed              * Comment this line if you want to ignore              * unsupported events              */             throw new IllegalArgumentException("Event not supported");         }     }      public static void main(String[] args) {         Foo foo = new Foo();         System.out.println(foo instanceof EventListener); // true         foo.onEvent(new LoginEvent()); // Login         foo.onEvent(new LogoutEvent()); // Logout     } } 

The suppress warnings are there because we are "abusing" type erasure and delegating to two different event listeners based on the event concrete type. I have chosen to do it using a HashMap and the run-time Event class, but there are a lot of other possible implementations. You could use anonymous inner classes like @user949300 suggested, you could include a getEventType discriminator on the Event class to know what do to with each event and so on.

By using this code for all effects you are creating a single EventListener able to handle two kinds of events. The workaround is 100% self-contained (no need to expose the internal EventListeners).

Finally, there is one last issue that may bother you. At compile time Foo type is actually EventListener. Now, API methods out of your control may be expecting parametrized EventListeners:

public void addLoginListener(EventListener<LoginEvent> event) { // ... // OR public void addLogoutListener(EventListener<LogoutEvent> event) { // ... 

Again, at run-time both of those methods deal with raw EventListeners. So by having Foo implement a raw interface the compiler will be happy to let you get away with just a type safety warning (which you can disregard with @SuppressWarnings("unchecked")):

eventSource.addLoginListener(foo); // works 

While all of this may seem daunting, just repeat to yourself "The compiler is trying to trick me (or save me); there is no spoon <T>. Once you scratch your head for a couple of months trying to make legacy code written before Java 1.5 work with modern code full of type parameters, type erasure becomes second nature to you.

like image 165
Anthony Accioly Avatar answered Oct 05 '22 07:10

Anthony Accioly


You need to use inner or anonymous classes. For instance:

class Foo {    public EventListener<X> asXListener() {       return new EventListener<X>() {           // code here can refer to Foo       };    }     public EventListener<Y> asYListener() {       return new EventListener<Y>() {           // code here can refer to Foo       };    } } 
like image 41
user949300 Avatar answered Oct 05 '22 07:10

user949300