TypeScript
Abstract classes
An abstract class cannot be instantiated and acts as a base template for subclasses. Use it to share common logic and enforce required methods.
Example
abstract class Shape {
abstract getArea(): number;
printArea() {
console.log("Area:", this.getArea());
}
}
class Circle extends Shape {
constructor(private radius: number) {
super();
}
getArea() {
return Math.PI * this.radius ** 2;
}
}
const circle = new Circle(10);
circle.printArea();Notes
abstractmethods have no implementation and must be overridden.new Shape()is not allowed.- Useful for class hierarchies with shared behavior.