What is wrong with the following code? Correct it. public class Circle{ private
ID: 3877698 • Letter: W
Question
What is wrong with the following code? Correct it.
public class Circle{
private double radius;
public Circle(double radius){
radius = radius;
}
public double getArea(){
return radius* radius*Math.PI;
}
class B extends Circle{
private double length;
B(double radius, double length)
{
Circle(radius);
length = length;
}
public double getArea(){
return getArea()*length;
}
}
Explanation / Answer
Answer:
The error in the code is in the base class constructor B (parameterized cosntructor) the first statement is you are trying to call super class constructor that is with statement Circle(radius) which was an error. With this statement compiler treats that Cirecle(double) is one of the method of class B. So to avoid this error replace this statement with
super(radius) which callls the base class parameterized constructor.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.