Java
Beginner
1 min read
Abstraction and Polymorphism
Example
import java.util.List;
// Abstract base class
abstract class Shape {
private String color;
Shape(String color) { this.color = color; }
public String getColor() { return color; }
// Abstract method — every concrete Shape must implement this
public abstract double area();
// Concrete method — shared by all shapes
public void describe() {
System.out.printf("%s [color=%s, area=%.2f]%n",
getClass().getSimpleName(), color, area());
}
}
class Circle extends Shape {
private final double radius;
Circle(String color, double radius) { super(color); this.radius = radius; }
@Override public double area() { return Math.PI * radius * radius; }
}
class Rectangle extends Shape {
private final double width, height;
Rectangle(String color, double width, double height) {
super(color);
this.width = width; this.height = height;
}
@Override public double area() { return width * height; }
}
class Triangle extends Shape {
private final double base, height;
Triangle(String color, double base, double height) {
super(color);
this.base = base; this.height = height;
}
@Override public double area() { return 0.5 * base * height; }
}
public class PolymorphismDemo {
public static void main(String[] args) {
List<Shape> shapes = List.of(
new Circle("red", 5),
new Rectangle("blue", 4, 6),
new Triangle("green", 3, 8)
);
double totalArea = 0;
for (Shape s : shapes) {
s.describe(); // dynamic dispatch — correct subtype called
totalArea += s.area();
}
System.out.printf("Total area: %.2f%n", totalArea);
}
}