Course contentsShow
← Software Engineering
Lesson 203 of 1,9336. SOLID PrinciplesFree lesson

Extension Through Inheritance

Learn how subclassing allows adding new behavior without modifying the base class implementation.

Extension Through Inheritance

You'll learn how subclassing lets you add new functionality while keeping the original code untouched.

The Core Idea

Inheritance allows you to create a new class (a subclass) based on an existing one (a superclass) without modifying the original code. The subclass automatically inherits all behaviors from the parent, and you can add new methods or override existing ones to extend functionality. This is a fundamental technique for satisfying the Open/Closed Principle: your base class remains closed for modification but open for extension.

How It Works

Think of a base class as a template or foundation. When you inherit from it, you're saying "I want everything the parent has, plus some additional behavior." The original class stays completely unchanged—no risk of breaking existing code—while your subclass adds the new features.

Analogy: Imagine a basic bicycle design. Instead of redesigning the original bicycle to add gears, you create a "mountain bike" version that extends the bicycle design by adding gear mechanisms. The original bicycle blueprint remains intact and continues working for anyone who needs just a simple bike.

Why This Matters

Extension through inheritance provides a safe way to grow your system:

  • The base class remains stable and reliable
  • Existing code using the base class continues working
  • New functionality lives in the subclass
  • Multiple subclasses can extend the same base differently

This technique protects you from the risk identified in "Why Modification is Risky"—you're not touching working code, just building on top of it.

Key Takeaway: Inheritance lets you extend a class's capabilities by creating a subclass that adds new behavior, leaving the original implementation unchanged and safe.