Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Inheritance

Reuse code and build relationships between classes.

Inheritance is a mechanism where a new class (derived class or subclass) inherits attributes and methods from an existing class (base class or superclass). This allows you to create a hierarchy of classes that share common functionality while allowing specialization.

Inheritance creates an “is-a” relationship. For example:

  • A Car is-a Vehicle
  • A Dog is-an Animal
  • A Manager is-an Employee
classDiagram
    class Vehicle {
        +brand: str
        +model: str
        +year: int
        +start() str
        +stop() str
        +get_info() str
    }
    
    class Car {
        +num_doors: int
        +honk() str
    }
    
    class Motorcycle {
        +bike_type: str
        +wheelie() str
    }
    
    Vehicle <|-- Car
    Vehicle <|-- Motorcycle

Subclasses can override parent methods to provide specialized behavior:

The super() function/keyword allows you to call methods from the parent class:

  • Inheritance creates an “is-a” relationship between classes
  • Subclasses inherit all attributes and methods from parent classes
  • Method overriding allows subclasses to provide specialized behavior
  • super() is used to call parent class methods
  • Multiple inheritance allows a class to inherit from multiple parents
  • MRO determines the order in which methods are resolved in multiple inheritance
  • Use inheritance to reuse code and create specialized classes from general ones

Inheritance is powerful for creating class hierarchies where specialized classes extend and customize the behavior of more general classes.

💡 Time to Practice!

Now that you understand the concepts, put them into practice with our interactive playground. Build UML diagrams, write code, and get AI-powered feedback.

Browse All Problems