A class implements a functional interface. This class has multiple methods with similar signatures, ie. they have an empty parameter list. A method that accepts this functional interface as an argument will accept any of those methods, even though they are not defined in the interface.
public class Frogs {
public static void main(String[] args) {
Froggo frog = new Froggo();
moveTheFrog(frog::move);
moveTheFrog(frog::jump);
moveTheFrog(frog::swim);
moveTheFrog(frog::croak);
}
static void moveTheFrog(Froginetic froginetic) {
froginetic.move();
}
@FunctionalInterface
interface Froginetic {
void move();
}
static class Froggo implements Froginetic {
@Override
public void move() { System.out.println("move"); }
void swim() { System.out.println("swim"); }
void jump() { System.out.println("jump"); }
void croak() {
System.out.println("croak");
System.exit(2460); // bucks}
}
}
}
Can't the compiler see that frog::jump is not defined in Froginetics? The designer of the moveTheFrog method would definitely not expect, nor account for, the effects of frog::croak.
It looks like good old fashioned C function ptrs are now a part of Java.
Is this broke? Or is this by design?