I am trying to better understand how method references work. In this example, I follow the logic of the code, but I don't understand the value of doing this. Basically, someMethod() is used as the implementation of the display() abstract method of MyInterface after the method reference assignment in STEP 2. But why? What are we gaining by doing this? When does this make sense architecturally? Seems that we take the Interface that has nothing to do with the class and associate them with this assignment.
@FunctionalInterface
interface MyInterface {
void display();
}
class DerivClass {
public void someMethod(){
System.out.println("Derived class method");
}
}
public class RefTest {
public static void main(String[] args) {
DerivClass dc = new DerivClass(); // STEP1: class instance
MyInterface myInterf = dc::someMethod; // STEP 2: assign method ref to interface
myInterf.display(); // STEP 3: executes someMethod(), prints "Derived class method"
}
}