Java — Which overriding declarations are valid in a subclass of class A? class A { protected int method1(int a, int b) { return 0; } }

Difficulty: Medium

Correct Answer: public int method1(int a, int b) { return 0; }

Explanation:


Introduction / Context:
This evaluates the rules of overriding regarding access modifiers, return types, and static usage in Java.



Concept / Approach:

  • Overridden methods can increase visibility (protected → public is valid).
  • Return type must be the same or covariant; short is not covariant with int.
  • Overridden methods cannot be static if the original was not static.
  • Access cannot be reduced (protected → private is invalid).


Step-by-Step:

Option A: Valid override (protected → public, same return type).Option B: Invalid (reduces visibility).Option C: Invalid (return type mismatch).Option D: Invalid (adds static to non-static method).


Final Answer:
public int method1(int a, int b) { return 0; }

More Questions from Declarations and Access Control

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion