0

I have this Java interface:

public interface MetronomeCallback {
    void onTick(boolean tickValue);
    void onBPM(int bpm);
}

public void setMetronomeCallback(MetronomeCallback metronomeCallback) {
    this.metronomeCallback = metronomeCallback;
}

This is a lambda function in java, I'm supposed to pass an object to setMetronomeCallback that implements these 2.

I want to implement it inline, like this:

       val metronomeCallback = MainService.MetronomeCallback {
            fun onTick(value: Boolean) {

            }

            fun onBPM(bpm: Int) {

            }
        }

        s.setMetronomeCallback(metronomeCallback);

How to create an object that implement these 2 functions?

Gatonito
  • 1,662
  • 5
  • 26
  • 55

1 Answers1

1

It's obvious when you know: to create an object that implements some interface, you use object : Interface { … }.

So in this case, I think you just need to insert object : before the interface name.

(You can extend a class similarly; you just need to add parens and any constructor parameters after the classname.  And you can inherit from multiple interfaces/classes, by separating them with a comma.)

The technical term for this is an object expression; it's all explained in the language docs.

gidds
  • 16,558
  • 2
  • 19
  • 26