Abstract Classes
Abstract Classes
Section titled “Abstract Classes”Introduction
Section titled “Introduction”An abstract class is a class that cannot be instantiated directly. It’s designed to be a base class that other classes extend. It can contain both abstract methods (no implementation) and concrete methods (with implementation).
Real-World Analogy: Vehicle Blueprint
Section titled “Real-World Analogy: Vehicle Blueprint”An abstract “Vehicle” blueprint:
- It defines shared parts every vehicle has (wheels, engine, steering) — concrete methods
- It says every vehicle must move(), but doesn’t specify how — abstract methods
- You can’t build a “Vehicle” directly (it’s abstract)
- You can build a “Car”, “Bike”, or “Truck” from it
Code Examples
Section titled “Code Examples”abstract class Report { constructor(protected title: string) {}
// Abstract methods — subclasses must implement abstract generate(): string; abstract getFormat(): string;
// Concrete method — shared behavior print(): void { console.log(`Printing report: ${this.title}`); }}
class PDFReport extends Report { generate(): string { return "PDF content"; } getFormat(): string { return "PDF"; }}from abc import ABC, abstractmethod
class Shape(ABC): @abstractmethod def area(self) -> float: pass
@abstractmethod def perimeter(self) -> float: pass
def describe(self) -> str: return f"Area: {self.area():.2f}, Perimeter: {self.perimeter():.2f}"When to Use Abstract Class vs Interface
Section titled “When to Use Abstract Class vs Interface”Use Abstract Class when:
- Classes share common state (fields)
- Classes share common code (concrete methods)
- Classes are closely related in hierarchy
Use Interface when:
- You want to define a capability across unrelated classes
- You need multiple inheritance of behavior
- You want a pure contract without any implementation
Previous Topic: Interfaces → Next Topic: Object Lifecycle →