0

When I tried to do Controller so, I`ve got a message: Listener cannot be inherited with different arguments AlfaMsg and GammaMsg. I read some patterns, but it does not help me. How I can realize this idea?

public abstract class Msg {

}   

public class AlfaMsg extends Msg{


}

public class GammaMsg extends Msg{


}

public interface Listener<T extends Msg> {
   void update(String message, T msg);    
}

public interface ListenerAlfa  extends Listener<AlfaMsg> {
   @Override    
   void update(String message, AlfaMsg deviceMessage);    
}

public interface ListenerGamma  extends Listener<GammaMsg> {
   @Override    
   void update(String message, GammaMsg deviceMessage);    
}

public class Controller implements ListenerAlfa, ListenerGamma{

    @Override
    void update(String message, AlfaMsg deviceMessage){

    }

    @Override
    void update(String message, GammaMsg deviceMessage){

    }
}

1 Answer 1

1

Unfortunately, you cannot do this using generics, because of type erasure - so that Controller would be implementing the same Listener interface twice.

You may want to review a previous answer to similar question, it has some workaround using encapsulation, but I am not sure this is what you'd like to implement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.