What is the output of the following application?
package mypkg; //ww w . ja v a 2 s . c om interface Printable { void print(double psi); } interface Displayable extends Printable { void display(double tensileStrength); } public class Rectangle { public static final void apply(Displayable instruction, double input) { // r1 instruction.display(input); } public static void main(String... future) { final Rectangle r = new Rectangle(); r.apply(x -> System.out.print(x+" bent!"), 5); } }
B.
A functional interface must contain exactly one abstract method.
The Displayable interface contains two abstract methods, print()
and display()
, since it extends Printable and inherits print()
.
The Displayable method is not a valid functional interface and therefore cannot be used as a lambda expression.
Option B is the correct answer.
The rest of the code compiles without issue.
The usage of an instance variable to call a static method, r.
apply()
in the main()
method, is permitted but discouraged.