0
Why I'm getting error??
public class main { public static void main(String[] args) { Shape s; s=new Circ(1); s.area(); s=new Rect(2,2); s.area(); System.out.println(s.area); public abstract class shape { public abstract double area(); class circ extends shape { private double ; public circ(double r) { this.r=r; } public double area() { return r*r*3.14; } } public class rect extends shape { private double h,w; public Rect(double h,w) { this.h=h; this.w=w; } public double area() { return h*w; } } } } }
1 Answer
0
You have the code format completely backwards. Here is a corrected version of your code step through it on the code playground page and see if you can find where you have been going wrong.
public class main
{
public static void main(String[] args) {
Shape circle = new Circ(1);
Shape rect = new Rect(2, 2);
System.out.println(circle.area());
System.out.println(rect.area());
}
}
public abstract class Shape {
public abstract double area();
}
public class Circ extends Shape {
private double r;
public Circ(double r) {
this.r=r;
}
public double area() {
return r*r*3.14;
}
}
public class Rect extends Shape {
private double h,w;
public Rect(double h, double w) {
this.h=h;
this.w=w;
}
public double area() {
return h*w;
}
}