Java OCA OCP Practice Question 1313

Question

What is the output of the following application?

package musical; 
interface Inter1 { default int talk() { return 7; } } 
interface Inter2 { default int talk() { return 5; } } 
public class Main implements Inter1, Inter2 { 
   public int talk(String... x) { 
      return x.length; 
   } //from   w  w  w .  j av a 2 s  .c  o m
   public static void main(String[] notes) { 
      System.out.print(new Main().talk(notes)); 
   } 
} 
  • A. 7
  • B. 5
  • C. The code does not compile.
  • D. The code compiles without issue, but the output cannot be determined until runtime.


C.

Note

Java does not allow multiple inheritance, so having one class extend two interfaces that both define the same default method signature leads to a compiler error, unless the class overrides the method.

In this case, though, the talk(String...) method defined in the Main class is not an overridden version of method defined in the interfaces because the signatures do not match.

The Main class does not compile since the class inherits two default methods with the same signature and no overridden version, making Option C the correct answer.




PreviousNext

Related