Difficulty: Medium
Correct Answer: public abstract class Circle extends Shape { private int radius; }
Explanation:
Introduction / Context:
This question tests knowledge of how to correctly extend an abstract class in Java and what it means for a subclass to remain abstract or become concrete. The abstract class Shape defines an abstract method draw and a concrete method setAnchor, so any non abstract subclass must provide an implementation of draw. The options show different ways of relating Circle to Shape, and you must determine which one is valid according to Java inheritance rules.
Given Data / Assumptions:
Concept / Approach:
In Java, a class extends another class and implements an interface. You cannot implement a class; the implements keyword is reserved for interfaces. If a subclass extends an abstract class but does not implement all its abstract methods, the subclass itself must be declared abstract. To become a concrete class that can be instantiated, the subclass must provide concrete implementations for all inherited abstract methods, including draw in this example. Also, method declarations in concrete classes must include method bodies, not just signatures ending with a semicolon.
Step-by-Step Solution:
Step 1: Examine option A: public class Circle implements Shape { private int radius; }. This is invalid because Shape is a class, and classes must be extended rather than implemented.Step 2: Examine option B: public abstract class Circle extends Shape { private int radius; }. This is valid because Circle extends Shape, inherits the abstract draw method, and remains abstract itself. It is not required to implement draw immediately because Circle is abstract.Step 3: Examine option C: public class Circle extends Shape { private int radius; public void draw(); }. This is invalid because draw is declared but not defined; in a concrete class the draw method must have a body.Step 4: Option D, None of the above, is unnecessary because option B is already a valid and typical way to extend Shape.Step 5: Therefore the only correct answer is option B, which defines Circle as an abstract subclass of Shape.
Verification / Alternative check:
If you attempt to compile option A, the compiler reports that Shape is a class and cannot be implemented. Trying to compile option C leads to an error because draw in a concrete class must have a method body. By contrast, option B compiles cleanly, as Java allows an abstract subclass to omit implementations of inherited abstract methods. A later concrete subclass of Circle can implement draw and become instantiable.
Why Other Options Are Wrong:
Option A misuses the implements keyword with a class, which is a common mistake when moving between interfaces and classes. Option C attempts to declare draw without a body in a concrete class, which violates Java syntax for non abstract methods. Option D is wrong because there is at least one valid option, namely B.
Common Pitfalls:
Developers sometimes forget to mark subclasses as abstract when they do not implement all abstract methods, leading to compilation errors. Another pitfall is mixing up implements and extends, especially when refactoring between interfaces and abstract base classes. Remember that interfaces are implemented and classes are extended, and that any class that omits implementations of inherited abstract methods must itself be abstract.
Final Answer:
The correct declaration is public abstract class Circle extends Shape { private int radius; }, which extends the abstract class Shape and remains abstract because it does not yet implement draw.
Discussion & Comments