Given the abstract class public abstract class Shape { private int x; private int y; public abstract void draw(); public void setAnchor(int x, int y) { this.x = x; this.y = y; } } which class declaration for Circle correctly extends Shape according to Java rules?

Difficulty: Medium

Correct Answer: public abstract class Circle extends Shape { private int radius; }

Explanation:


Introduction / Context:
This question is another way of asking how to correctly define a subclass of an abstract class in Java. It uses the same abstract Shape class and asks you to evaluate several candidate Circle declarations. The focus is on the proper use of extends versus implements and on handling inherited abstract methods correctly.


Given Data / Assumptions:

    • Shape is an abstract class with an abstract method draw and a concrete method setAnchor.
    • Circle should be related to Shape as a subclass rather than as an interface implementation.
    • Java requires that all abstract methods be implemented in concrete subclasses or that the subclass remain abstract.
    • The candidate Circle declarations use different combinations of implements, extends, and method bodies.


Concept / Approach:
Java uses the extends keyword to create a subclass of a class and the implements keyword to indicate that a class implements one or more interfaces. You cannot implement a class. When a class extends an abstract class that has abstract methods, it must either provide concrete implementations for those methods or be declared abstract itself. Method declarations in a concrete class must include bodies, whereas abstract methods end with a semicolon and appear only in abstract classes or interfaces.


Step-by-Step Solution:
Step 1: Option A uses public class Circle implements Shape { private int radius; }. Shape is a class, so it must be extended, not implemented, which makes this declaration invalid.Step 2: Option B uses public abstract class Circle extends Shape { private int radius; }. This is valid because Circle extends the abstract class Shape and may choose not to implement draw immediately, remaining abstract itself.Step 3: Option C uses public class Circle extends Shape { private int radius; public void draw(); }. This is invalid because draw is declared without a method body, yet Circle is a concrete class.Step 4: Option D claims that none of the options are correct, but we have already identified option B as valid.Step 5: Therefore option B is the correct answer because it correctly extends Shape and respects the abstract method draw.


Verification / Alternative check:
Compiling a Java source file that contains the Shape class and the Circle class from option B shows that the code compiles without error. Replacing Circle with the option A version leads to a compile time message that a class cannot implement another class. Using the option C version causes a compile error that draw must be declared abstract or must have a body, because Circle is not abstract. These compiler messages confirm that option B is the only correct declaration.


Why Other Options Are Wrong:
Option A misuses the implements keyword with a class rather than an interface. Option C mixes concrete and abstract method syntax in a concrete class, which is not allowed. Option D fails because there is a valid option, namely the abstract subclass in option B.


Common Pitfalls:
Programmers sometimes attempt to implement classes as if they were interfaces, which is a syntax error in Java. Another frequent oversight is forgetting to mark a subclass abstract when it does not implement all abstract methods from its parent, leading to errors about unimplemented methods. Always check whether you are working with a class or an interface and ensure that abstract methods are either implemented or carried forward in an abstract subclass.


Final Answer:
The correct declaration is public abstract class Circle extends Shape { private int radius; }, which properly extends the abstract class Shape and remains abstract until draw is implemented.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion