Bird
0
0

Identify the error in the following code:

medium📝 Debug Q14 of 15
Java - Abstraction
Identify the error in the following 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();
    }
}
ASquare class must implement the abstract method draw()
BCannot create object of abstract class Shape
CMethod draw() in Circle should be abstract
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check abstract method implementation in subclasses

    The abstract method draw() in Shape must be implemented by all non-abstract subclasses.
  2. Step 2: Verify Square class implementation

    The Square class does not implement draw() and is not declared abstract, causing a compilation error.
  3. Final Answer:

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

    All abstract methods must be implemented in concrete subclasses [OK]
Quick Trick: All abstract methods must be implemented or class must be abstract [OK]
Common Mistakes:
  • Forgetting to implement abstract methods
  • Thinking abstract class objects can be created
  • Marking implemented methods as abstract

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes