What is the output of the following code? (Choose all that apply)
1: interface MyInterface { int getValue(); } 2: abstract class MyClass implements MyInterface { 3: protected int getValue() {return 4;} 4: } 5: public class MyClass2 extends MyClass { 6: public static void main(String[] args) { 7: MyClass puma = new MyClass(); 8: System.out.println(puma.getValue()); 9: } 10: 11: public int getValue(int length) {return 2;} 12: }
C, D, E.
The method getValue() in the interface MyInterface is assumed to be public, since it is part of an interface.
C is correct. The implementation of the method on line 3 is therefore an invalid override, as protected is a more restrictive access modifier than public.
D is correct. class MyClass2 implements an overloaded version of getValue(), since the declaration in class MyClass is invalid, it needs to implement a public version of the method. Since it does not, the declaration of MyClass is invalid.
Option E is correct, since MyClass is marked abstract and cannot be instantiated.
F is not correct. The overloaded method on line 11 is declared correctly.
As the code has compiler errors, A, B, and G are not correct.