Bird
0
0

Identify the error in the following Java code:

medium📝 Debug Q14 of 15
Java - Abstraction
Identify the error in the following Java code:
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square extends Shape {
}

public class Test {
    public static void main(String[] args) {
        Shape s = new Square();
        s.draw();
    }
}
AMethod draw() in Circle should be abstract
BCannot create object of abstract class Shape
CClass Square must implement the abstract method draw()
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check abstract method implementation in subclasses

    Class Shape has abstract method draw(). Circle correctly implements it. Square does not implement draw().
  2. Step 2: Understand consequences of missing implementation

    Since Square does not implement the abstract method, it must be declared abstract or implement the method. Otherwise, compilation error occurs.
  3. Final Answer:

    Class Square must implement the abstract method draw() -> Option C
  4. Quick Check:

    Subclass must implement all abstract methods [OK]
Quick Trick: All abstract methods must be implemented or subclass declared abstract [OK]
Common Mistakes:
  • Forgetting to implement abstract methods in subclasses
  • Trying to instantiate abstract classes directly
  • Misunderstanding abstract method requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes