Concrete Methods in Abstract Classes
What you'll learn: Abstract classes can include fully implemented methods alongside abstract ones, letting you define shared behavior once for all subclasses.
The Core Idea
While abstract classes must include at least one abstract method that subclasses implement, they can also contain concrete methods—methods with complete implementations. This gives you a powerful middle ground: you can define shared behavior that all subclasses inherit automatically, while still requiring certain methods to be customized per subclass.
Think of an abstract class as a blueprint with some parts filled in and others left blank. The concrete methods are the "filled in" parts—common behavior you don't want to rewrite in every subclass.
How It Works
abstract class Vehicle {
constructor(protected brand: string) {}
// Abstract method - must be implemented by subclasses
abstract startEngine(): void;
// Concrete method - shared implementation
displayInfo(): void {
console.log(`This is a ${this.brand} vehicle`);
}
}
class Car extends Vehicle {
startEngine(): void {
console.log(`${this.brand} car engine started`);
}
// displayInfo() is inherited automatically
}
const myCar = new Car("Toyota");
myCar.displayInfo(); // "This is a Toyota vehicle"
myCar.startEngine(); // "Toyota car engine started"
The displayInfo() method is concrete—it has a full implementation that works for all vehicles. Every subclass inherits this behavior without needing to rewrite it.
Why This Matters
This pattern eliminates code duplication. If ten vehicle types all need to display info the same way, you write it once in the abstract class rather than ten times. You get to share the common logic while still enforcing customization where it matters (like startEngine()).
Key Takeaway: Abstract classes can mix abstract methods (must be implemented) with concrete methods (already implemented and shared) to balance code reuse with enforced customization.