Java OCA OCP Practice Question 1268

Question

What is the output of the following application?

package mypkg; /*from w  ww. j a  v  a  2s . com*/
interface Run { 
   default void walk() { 
      System.out.print("A and B!"); 
   } 
} 
interface Jog { 
   default void walk() { 
      System.out.print("A and C!"); 
   } 
}? 
public class Main implements Run, Jog { 
   public void walk() { 
      System.out.print("D!"); 
   } 
   public static void main(String[] argv) { 
      new Main().walk(); 
   } 
} 
  • A. A and B!
  • B. A and C!
  • C. D!
  • D. The code does not compile.


C.

Note

Having one class implement two interfaces that both define the same default method signature leads to a compiler error, unless the class overrides the default method.

In this case, the Main class does override the walk() method correctly, therefore the code compiles without issue, and Option C is correct.




PreviousNext

Related