Bird
0
0

Find the error in the following code snippet:

medium📝 Debug Q14 of 15
Java - Inheritance

Find the error in the following code snippet:

class Parent {
    final void show() {
        System.out.println("Parent show");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("Child show");
    }
}
AParent class cannot have final methods
BChild class must declare show() as final
CChild class cannot override final method show()
DNo error, code is valid
Step-by-Step Solution
Solution:
  1. Step 1: Understand final method behavior

    Methods declared final in a parent class cannot be overridden in child classes.
  2. Step 2: Analyze Child class method

    Child class tries to override final method show(), which causes a compile-time error.
  3. Final Answer:

    Child class cannot override final method show() -> Option C
  4. Quick Check:

    final methods block overriding [OK]
Quick Trick: final methods cannot be overridden in subclasses [OK]
Common Mistakes:
  • Thinking final methods can be overridden
  • Assuming no error in overriding final methods
  • Confusing final methods with abstract methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes