[Java] Can't convert event subclasses to Event superclass

Maybe it's just me doing something wrong, but when using EventBus.listenTo() I need to pass two arguments: an Event subclass, which points to the type of the event to be handled, and a Function1<Event,BoxedUnit>, which is a handler itself. The problem is: if I make a handler of any event besides default Event, I get the incompatible type error.

The problem example

Let's say I want to create a BeepEvent listener just as in the provided demo. In Java it should look like

EventBus.listenTo(BeepEvent.class, /*listener*/);

If I create the listener using an anonymous subclass, i.e.:

EventBus.listenTo(BeepEvent.class, new BeepEventFunction() {
  @Override
  public BoxedUnit apply(BeepEvent v1){
    System.out.println("[EVENT] Beep (address = "+v1.address()+", frequency = "+v1.frequency()+", duration = "+v1.duration()+")");
    return null;
  }
});

/* Or if we use lambda expression */
EventBus.listenTo(BeepEvent.class, (BeepEvent v1) -> {
  System.out.println("[EVENT] Beep (address = "+v1.address()+", frequency = "+v1.frequency()+", duration = "+v1.duration()+")");
  return null;
});

// TL;DR

abstract class BeepEventFunction implements Function1<BeepEvent, BoxedUnit>{}

I get the error

incompatible types: <anonymous Main.BeepEventFunction> cannot be converted to Function1<Event,BoxedUnit>

If I do the same using a normal subclass, i.e.:

EventBus.listenTo(BeepEvent.class, new BeepEventFunction());

// TL;DR

class BeepEventFunction implements Function1<BeepEvent, BoxedUnit>{
  @Override
  public BoxedUnit apply(BeepEvent v1){
    System.out.println("[EVENT] Beep (address = "+v1.address()+", frequency = "+v1.frequency()+", duration = "+v1.duration()+")");
    return null;
  }
}

I get the error

incompatible types: BeepEventFunction cannot be converted to Function1<Event,BoxedUnit>

If I change BeepEvent to Event the errors are gone, but now I'm not able to get event info:

EventBus.listenTo(BeepEvent.class, (Event v1) -> {
  System.out.println("[EVENT] Beep (address = "+v1.address()+", frequency = "+v1.frequency()+", duration = "+v1.duration()+")"); 
  //Can't find method: address(), location: variable v1 of type Event
  //Can't find method: frequency(), location: variable v1 of type Event
  //Can't find method: duration(), location: variable v1 of type Event
  return null;
});
Edited Feb 04, 2021 by VladTheMountain
Assignee Loading
Time tracking Loading