Abstract class
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
double radius;
Circle(double r) {
radius = r;
}
double area() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
double length;
double width;
Rectangle(double l, double w) {
length = l;
width = w;
}
double area() {
return length * width;
}
}
// Main class
public class AbstractClassExample {
public static void main(String[] args) {
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("Area of Circle: " + circle.area());
System.out.println("Area of Rectangle: " + rectangle.area());
}
}
0 Comments