0

I have some code like so:

private void processMessage(MessageType message, MessageHandler<MessageType> handler)
{
    handler.handleMessage(message);
}

With message handler being an abstract class.

public abstract class MessageHandler <T extends MessageType>
{
    public abstract T handleMessage(T message);
}

I have a child class:

public class FooBarHandler extends MessageHandler<FooBarMessageType>
{
    public FooBarMessageType handleMessage(FooBarMessageType message)
    {...}
}

Where FooBarMessageType is a subclass of MessageType.

Now, when I call processMessage as such:

processMessage((FooBarMessageType)message, new FooBarHandler());

I get this error in eclipse:

The method processMessage(MessageType, MessageHandler) in the type StuffDoer is not applicable for the arguments (FooBarMessageType, FooBarHandler)

I noticed that when I made the second formal parameter for processMessage raw:

private void processMessage(MessageType message, MessageHandler handler)

It just becomes a warning. However, the only solution eclipse gives to fix this warning generates the error again.

  • [Is List a subclass of List? Why are Java generics not implicitly polymorphic?](https://stackoverflow.com/q/2745265), note that `FooBarHandler extends MessageHandler` but `MessageHandler` is not subtype of `MessageHandler` as explained in linked question. – Pshemo Apr 07 '23 at 17:03
  • What is `StuffDoer`? Your abstract class or your concrete class? If it's your abstract class, then you're using a reference with the wrong generic type. – khelwood Apr 07 '23 at 17:06
  • @Pshemo that question was entirely unhelpful. However, the issue was resolved by adding a type parameter to processMessage. private void processMessage(T message, MessageHandler handler) However you were hasty for flagging and didn't even let me submit a self answer. – Kyle Dumouchelle Apr 07 '23 at 17:14
  • @KyleDumouchelle What makes you think I flag your question? Also how understanding cause of problem is "entirely unhelpful" in finding a way to solve it? – Pshemo Apr 07 '23 at 17:18

0 Answers0