Bird
0
0

Identify the error in this code related to runtime polymorphism:

medium📝 Debug Q6 of 15
Java - Polymorphism
Identify the error in this code related to runtime polymorphism:
class Base { void show() { System.out.println("Base"); } } class Derived extends Base { static void show() { System.out.println("Derived"); } } public class Test { public static void main(String[] args) { Base b = new Derived(); b.show(); } }
ACannot override static method with instance method
BStatic method cannot override instance method
CNo error, output is Derived
DCompilation error due to method signature mismatch
Step-by-Step Solution
Solution:
  1. Step 1: Check method types in base and derived classes

    Base has instance method show(), Derived has static method show().
  2. Step 2: Understand overriding rules for static methods

    Static methods cannot override instance methods; this is method hiding, not overriding.
  3. Final Answer:

    Static method cannot override instance method -> Option B
  4. Quick Check:

    Static methods cannot override instance methods [OK]
Quick Trick: Static methods hide, do not override instance methods [OK]
Common Mistakes:
  • Thinking static methods override instance methods
  • Expecting Derived's method to be called polymorphically
  • Ignoring method hiding vs overriding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes