Abstraction & Abstract Classes
Prerequisites: Polymorphism
An abstract class can declare methods with no body, forcing
every concrete subclass to provide its own implementation — and it
can never be instantiated directly with new.
abstract class Shape {
abstract double area(); // no body — subclasses must implement it
void describe() {
System.out.println("Area: " + area());
}
}
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
}
Common mistakes
- Trying
new Shape()directly — abstract classes exist to be extended, not instantiated.