Given: 10. class One { 11. void foo() {} 12. } 13. class Two extends One { 14. //insert method here 15. } Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.)

  1. int foo() { /* more code here */ }

  2. void foo() { /* more code here */ }

  3. public void foo() { /* more code here */ }

  4. protected void foo() { /* more code here */ }


Correct Option: B,C,D

AI Explanation

To answer this question, we need to understand the concept of method overriding and access modifiers in Java.

In Java, when a class extends another class, it inherits all the methods from the superclass. However, if the subclass needs to provide a different implementation of a method inherited from the superclass, it can override the method.

In this scenario, class Two extends class One, which means that it inherits the method foo() from class One. To correctly complete class Two, we need to override the foo() method with the correct access modifier.

Let's go through each option to understand why it is correct or incorrect:

Option A) int foo() { /* more code here */ } This option is incorrect because it changes the return type of the foo() method, which is not allowed when overriding a method.

Option B) void foo() { /* more code here */ } This option is correct because it overrides the foo() method with the same return type and access modifier.

Option C) public void foo() { /* more code here */ } This option is correct because it overrides the foo() method with the same return type and a broader access modifier (public is broader than the default access modifier).

Option D) protected void foo() { /* more code here */ } This option is correct because it overrides the foo() method with the same return type and a broader access modifier (protected is broader than the default access modifier).

The correct answers are B), C), and D). These options correctly complete class Two by overriding the foo() method with the same return type and appropriate access modifiers.

Find more quizzes: