I'm trying to pass a subclass as a parameter to a method. So far I've not been successfull, and even told it's not possible to do in Java. I want the opinion of stackoverflow, and suggestions to make it work.
Let's assume 'HelloEvent' extends 'Event'...
I have a method :
public void addListener(Consumer<? extends Event> consumer) {
[...]
}
and another method :
public void listen(HelloEvent helloEvent) {
[...]
}
I want to do this :
addListener(this::listen);
In IDEA I have the error 'Cannot resolve method listen'. Of course this happens because 'listen' is not exactly an Event, but rather a subclass.
Is there a way to do this? Maybe a work around?
I've tried having it being a Function or replacing 'extends' with 'super' and it does not work. I've been attempting to fix this problem for a few weeks now.