Bird
0
0

Identify the error in this code:

medium📝 Debug Q6 of 15
Java - Abstraction
Identify the error in this code:
abstract class Animal {
    abstract void sound();
}
class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}
class Cat extends Animal {
}
public class Test {
    public static void main(String[] args) {
        Animal a = new Cat();
        a.sound();
    }
}
ADog class must be declared abstract.
BNo error; code runs fine.
CAnimal class cannot have abstract methods.
DCat class must implement sound() method or be abstract.
Step-by-Step Solution
Solution:
  1. Step 1: Check Cat class implementation

    Cat extends Animal but does not implement abstract method sound().
  2. Step 2: Understand abstract method rules

    Subclass must implement all abstract methods or be declared abstract; Cat is neither.
  3. Final Answer:

    Cat class must implement sound() method or be abstract. -> Option D
  4. Quick Check:

    Abstract method implementation required = B [OK]
Quick Trick: All abstract methods must be implemented or subclass abstract [OK]
Common Mistakes:
  • Ignoring missing method in subclass
  • Thinking abstract class can't have abstract methods
  • Assuming code compiles without all methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes