Java OCA OCP Practice Question 2107

Question

determine the behavior of this program:.

interface Interface1 {
    default void foo() { System.out.println("Interface1's foo"); }
}

interface DerivedInterface extends Interface1 {
    default void foo() { System.out.println("DerivedInterface's foo"); }
}

interface AnotherInterface {
    public static void foo() { System.out.println("AnotherInterface's foo"); }
}

public class MultipleInheritance implements DerivedInterface, AnotherInterface {
    public static void main(String []args) {
        new MultipleInheritance().foo();
    }/*from  ww  w.ja  v a  2 s .com*/
}
A.  this program will result in a compiler error: 
    redundant method definition for function foo
B.  this program will result in a compiler error 
    in Multipleinheritance class:ambiguous call to function foo
C.  the program prints: derivedinterface's foo
D.  the program prints: anotherinterface's foo


C.

Note

a default method can be overridden.

since derivedinterface extends Baseinterface, the default method definition for foo is overridden in the derivedinterface.

static methods do not cause conflicting definition of foo since they are not overridden, and they are accessed using the interface name, as in anotherinterface.foo.

hence, the program compiles without errors.

in the main method within Multipleinheritance class, the overridden foo method is invoked and hence the call resolves to foo method defined in derivedinterface.




PreviousNext

Related