OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

Which of the statements given in options are true about method overriding or method hiding?

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Which of the statements given in options are true about method overriding or method hiding?

I) Subclass return type should be same as super class method return type.

II) Static modifier should not be added or removed in subclass method.

III) In subclass method accessibility modifiers should be same as super class method or it can be increased, but should not be decreased.

Choose one option.
Show answer & explanation
Answer: B. Only (II) and (III)

Statement I is FALSE: Java allows covariant return types (subclass method can return a type that is a subtype of the superclass return type), introduced in Java 5. Statements II and III are TRUE: static modifiers cannot be changed between superclass and subclass (this becomes method hiding, not overriding), and accessibility modifiers can only stay the same or be increased (made less restrictive), never decreased.

Step-by-step Derivation:
Analyzing each statement:

I) Return Type Covariance:

  • FALSE in strict terms. While return types must be compatible, Java 5+ allows covariant return types.
  • Example: If superclass returns Object, subclass can return String (a subclass of Object).
  • The rule is NOT that they must be identical.

II) Static Modifier Invariance:

  • TRUE. If superclass method is non-static, subclass method must also be non-static (method overriding).
  • If superclass method is static, subclass method must also be static (method hiding).
  • Changing static modifier violates override contract and causes compilation errors or unexpected behavior.

III) Accessibility Modifier Rules:

  • TRUE. Accessibility can only remain the same or increase (become less restrictive):
    • public → can stay public
    • protected → can become public or stay protected
    • package-private → can become public, protected, or stay package-private
    • private → cannot be overridden (it's not visible to subclass)
  • Cannot decrease: protected → package-private is invalid.

Conclusion: Statements II and III are correct. Answer is B.