1Z0-809 · Question #62
Given the following abstract class Shape: ``java 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 =…
The correct answer is B. public abstract class Circle extends Shape { Private int radius; } E. public class Circle extends Shape { Private int radius; public void draw() { /* code here */ } }. B and E are correct because Shape is an abstract class, not an interface - this means subclasses must use extends, not implements. Option B is valid because a class declared abstract is not required to implement inherited abstract methods (it defers that responsibility to its…
Question
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 two classes use the shape class correctly?Options
- Apublic abstract class Circle implements Shape { Private int radius; }
- Bpublic abstract class Circle extends Shape { Private int radius; }
- Cpublic class Circle extends Shape { Private int radius; public void draw(); }
- Dpublic abstract class Circle implements Shape { private int radius; public void draw(); }
- Epublic class Circle extends Shape { Private int radius; public void draw() { /* code here */ } }
- Fpublic abstract class Circle implements Shape { private int radius; public void draw() { /* code here */ } }
How the community answered
(29 responses)- A3% (1)
- B72% (21)
- C14% (4)
- D3% (1)
- F7% (2)
Explanation
B and E are correct because Shape is an abstract class, not an interface - this means subclasses must use extends, not implements. Option B is valid because a class declared abstract is not required to implement inherited abstract methods (it defers that responsibility to its own subclasses). Option E is valid because it's a concrete class that properly implements draw() with a method body, satisfying the contract imposed by the abstract parent.
Why the distractors fail:
- A, D, F - all use
implements Shape, which is illegal; youimplementinterfaces andextendclasses. SinceShapeis an abstract class, this is a compile-time error. - C - writes
public void draw();with no body in a concrete class. A method signature without a body is only legal in an interface or when explicitly markedabstract. A concrete class extending an abstract parent must provide a full implementation.
Memory tip: "Classes extend, interfaces implement." When you see abstract class in the question, immediately cross out every answer that says implements - you've already eliminated half the wrong choices.
Community Discussion
No community discussion yet for this question.