0

I have this code:

MessageConsumer<String> handler = consumer.handler(handleBusRequest());
MessageConsumer<Object> handler2 = (MessageConsumer<String>) handler;

But cast it is not allowed. How should I do it?

David Marciel
  • 865
  • 1
  • 12
  • 29
  • 4
    You shouldn't do that, for [the same reasons that the compiler doesn't allow it](https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-are-java-generics-not-implicitly-po). If you know what you're doing, then you [can still cast](https://stackoverflow.com/questions/3246137/java-generics-cannot-cast-listsubclass-to-listsuperclass) – ernest_k Jan 29 '20 at 08:45
  • You can do List extends String> handler2 = handler; – David Marciel Jan 29 '20 at 10:53

0 Answers0