What is the output of the following application?
package mypkg; // w w w.j a v a 2s . c o m interface Plant { default String grow() { return "A!"; } } interface Living { public default String grow() { return "B!"; } } public class Main implements Plant, Living { // m1 public String grow(int height) { return "Super B!"; } public static void main(String[] leaves) { Plant p = new Main(); // m2 System.out.print(((Living)p).grow()); // m3 } }
D.
A class cannot inherit two interfaces that declare the same default method, unless the class overrides them.
The version of grow()
in the Main class is an overloaded method, not an overridden one.
The code does not compile due to the declaration of Main on line m1, and Option D is the correct answer.