Java SE 8 Programmer II — Question 55

Given:
class Bird {
public void fly () { System.out.print("Can fly"); }
}
class Penguin extends Bird {
public void fly () { System.out.print("Cannot fly"); }
}
and the code fragment:
class Birdie {
public static void main (String [ ] args) {
fly( ( ) -> new Bird ( ));
fly (Penguin : : new);
}
/* line n1 */
}
Which code fragment, when inserted at line n1, enables the Birdie class to compile?

Answer options

Correct answer: C

Explanation

The correct answer is C, as it utilizes a Supplier<Bird> which allows for the creation of a Bird object and then calls the fly method on it. Option A and B incorrectly use Consumer, which does not suit the context as they expect a method reference that is not applicable here. Option D also fails since it uses a method reference incorrectly and does not compile as intended.