Bird
0
0

Identify the problem in this Java code:

medium📝 Debug Q7 of 15
Java - Object-Oriented Programming Concepts
Identify the problem in this Java code:
class Animal {
  void sound() {
    System.out.println("Animal sound");
  }
}
class Dog extends Animal {
  void sound(int volume) {
    System.out.println("Bark at volume " + volume);
  }
}
public class Test {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.sound();
  }
}
AAnimal class cannot be extended
BMethod sound() in Animal is private
CDog's sound method does not override Animal's sound method
DDog class must declare a constructor
Step-by-Step Solution
Solution:
  1. Step 1: Check method overriding rules

    Method overriding requires same method name and parameters. Dog's sound(int) is an overload, not override.
  2. Step 2: Analyze method call

    Variable 'a' is Animal type, calls sound() with no parameters, so Animal's method runs, not Dog's.
  3. Final Answer:

    Dog's sound method does not override Animal's sound method -> Option C
  4. Quick Check:

    Overriding needs exact method signature [OK]
Quick Trick: Overriding requires same method signature [OK]
Common Mistakes:
  • Confusing overloading with overriding
  • Assuming Animal cannot be extended
  • Thinking private methods are involved

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes