Abstract Classes and Methods
What
An abstract class is a base class that cannot be instantiated directly. It serves as a template, forcing any subclasses to implement specific “abstract” methods.
How
You mark the class and any methods that lack an implementation as abstract:
abstract class Shape {
abstract calculateArea(): number; // No implementation here
}
class Circle extends Shape {
calculateArea() { return Math.PI * 5 * 5; } // Required implementation
}
Why
Abstract classes are powerful for defining strict contracts in a class hierarchy. They allow you to provide shared logic (in non-abstract methods) while ensuring that every specific subclass provides its own required logic for core operations.
Summary
Abstract classes are ideal for creating “templates” where some parts are shared and others must be customized by subclasses.