Java OCA OCP Practice Question 805

Question

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 
   } 
} 
  • A. A!
  • B. B!
  • C. Super B!
  • D. It does not compile because of line m1.
  • E. It does not compile because of line m2.
  • F. It does not compile because of line m3.


D.

Note

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.




PreviousNext

Related