Bird
0
0

Which of the following is the correct syntax to override a method in Java?

easy📝 Syntax Q12 of 15
Java - Inheritance

Which of the following is the correct syntax to override a method in Java?

class Parent {
    void show() { System.out.println("Parent"); }
}
class Child extends Parent {
    ?
}
Aprivate void show() { System.out.println("Child"); }
B@Override void show() { System.out.println("Child"); }
Cvoid Show() { System.out.println("Child"); }
Dvoid show(int x) { System.out.println("Child"); }
Step-by-Step Solution
Solution:
  1. Step 1: Check method signature and annotation

    To override, method name and parameters must match exactly. Using @Override annotation helps catch mistakes.
  2. Step 2: Analyze options

    private void show() { System.out.println("Child"); } uses private access modifier, narrowing from package-private (not allowed for overriding; causes hiding). void show(int x) { System.out.println("Child"); } changes parameters (overloading). void Show() { System.out.println("Child"); } has method name mismatch ('Show' vs 'show' due to case sensitivity). @Override void show() { System.out.println("Child"); } matches method signature exactly and uses @Override correctly.
  3. Final Answer:

    @Override void show() { System.out.println("Child"); } -> Option B
  4. Quick Check:

    @Override + same signature = correct override [OK]
Quick Trick: Use @Override and same method signature to override [OK]
Common Mistakes:
  • Narrowing access modifier (e.g., to private)
  • Changing method parameters (overloading instead)
  • Method name case mismatch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes