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? (Choose two.)Select one or more:a.public class Circle implements Shape { private int radius;}b.public class Circle extends Shape { private int radius; public void draw();}c.public abstract class Circle extends Shape { private int radius;}d.public class Circle extends Shape { private int radius; public void draw() {/* code here */}}
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? (Choose two.)Select one or more:a.public class Circle implements Shape { private int radius;}b.public class Circle extends Shape { private int radius; public void draw();}c.public abstract class Circle extends Shape { private int radius;}d.public class Circle extends Shape { private int radius; public void draw() {/* code here */}}
Solution
The correct answers are:
c. public abstract class Circle extends Shape { private int radius;}
d. public class Circle extends Shape { private int radius; public void draw() {/* code here */}}
Explanation:
Option a is incorrect because it uses 'implements' instead of 'extends'. In Java, 'implements' is used for interfaces, not abstract classes.
Option b is incorrect because it declares the method 'draw()' without providing a body. In Java, if you're not making the class abstract, you need to provide a body for all the methods of the superclass.
Option c is correct because it extends the abstract class 'Shape' and doesn't need to provide a body for the 'draw()' method since the 'Circle' class itself is declared as abstract.
Option d is correct because it extends the abstract class 'Shape' and provides a body for the abstract method 'draw()'.
Similar Questions
Which would declare a compilable abstract class?Select one:a.public abstract class Shape { public Square draw(); }b.public abstract class Shape { public Square draw() { } }c.public class Shape abstract { public abstract Square draw(); }d.public class Shape { public abstract Square draw(); }
A class Circle is defined in the following code:public class Circle { int radius; static String name; void getPerimeter() { } static void getArea() { }}Let c be an instance of Circle, which of the statements are correct?System.out.println(c.radius);c.getArea();c.getPerimeter();System.out.println(c.name);
Find OutputType the correct output for the code given below. Also, explain the reason behind your answer.class Shape { public Shape() { System.out.println("inside Shape class default constructor"); }}class Rectangle extends Shape { public Rectangle() { System.out.println("inside Rectangle class default constructor"); }}class Square extends Rectangle { public Square() { System.out.println("inside Square class default constructor"); }}public class ConstructorChain { public static void main(String[] args) { Square sq = new Square(); }}
Observe the below code : abstract public class Account{ private int accountNumber; private String holderName; private double balance; public Account(int accountNumber, String holderName, double balance) { this.accountNumber = accountNumber; this.holderName = holderName; this.balance = balance; } }Identify the correct statements.Select one:a.Code compiles successfullyb.code will not compile, because abstract class cannot have a constructorc.code will not compile because the abstract class do not have abstract methods
Consider the following class diagram. Which class correctly implements the Drawable interface?a.class Circle { int radius; void draw() {} }b.class Circle implements Drawable { int radius; void draw() {} }c.class Circle implements Drawable { int radius; }d.class Circle implements Drawable { void draw() {} }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.