Skip to main content

Concept

Inheritance is a mechanism to create a new class (subclass) that inherits attributes and methods from an existing class (parent class). It allows for code reuse and establishes a relationship between classes.
TypeDescriptionExample
Single InheritanceA subclass inherits from one parent class.class Dog(Animal): pass
Multiple InheritanceA subclass inherits from multiple parent classes.class FlyingDog(Dog, Bird): pass
Multilevel InheritanceA chain of inheritance across three or more levels.Animal → Mammal → Dog
Hierarchical InheritanceMultiple subclasses inherit from a single parent class.Animal → Dog, Cat, Bird
Hybrid InheritanceA combination of two or more types of inheritance.Dog and Bird both extend Animal; FlyingDog extends both.

Implementation

A subclass inherits from one parent class. Use super().__init__() to call the parent constructor.
animal.py
class Animal:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def speak(self):
    print("Animal speaks")

class Dog(Animal):
  def __init__(self, name, age):
    super().__init__(name, age)  # Call the parent class constructor

  def speak(self):  # Method overriding
    print("Woof!")

dog = Dog("Buddy", 3)
dog.speak()                      # Output: Woof!
print(dog.name)                  # Output: Buddy
print(dog.age)                   # Output: 3
print(isinstance(dog, Animal))   # Output: True
super().__init__(name, age) calls the constructor of the parent class (Animal) to initialize inherited attributes. isinstance(dog, Animal) returns True because Dog is a subclass of Animal.
A subclass inherits from multiple parent classes. The order of method resolution is determined by the Method Resolution Order (MRO), which follows the C3 linearization algorithm.
animal.py
class Dog:
  def bark(self):
    print("Woof!")

class Bird:
  def fly(self):
    print("Bird flies")

class FlyingDog(Dog, Bird):
  pass

fd = FlyingDog()
fd.bark()  # Output: Woof!
fd.fly()   # Output: Bird flies
The MRO for FlyingDog is: FlyingDog → Dog → BirdYou can inspect MRO at runtime:
print(FlyingDog.__mro__)
# (<class 'FlyingDog'>, <class 'Dog'>, <class 'Bird'>, <class 'object'>)
A chain where a subclass inherits from a parent, which itself inherits from another parent.
animal.py
class Animal:
  def eat(self):
    print("Animal eats")

class Mammal(Animal):
  pass

class Dog(Mammal):
  pass

dog = Dog()
dog.eat()  # Output: Animal eats
Multiple subclasses share a single parent class.
animal.py
class Animal:
  def sleep(self):
    print("Animal sleeps")

class Dog(Animal):
  pass

class Cat(Animal):
  pass

dog = Dog()
cat = Cat()
dog.sleep()  # Output: Animal sleeps
cat.sleep()  # Output: Animal sleeps
A combination of multiple inheritance types. Here Dog and Bird both extend Animal, and FlyingDog extends both.
animal.py
class Animal:
  def move(self):
    print("Animal moves")

class Dog(Animal):
  def bark(self):
    print("Woof!")

class Bird(Animal):
  def fly(self):
    print("Bird flies")

class FlyingDog(Dog, Bird):
  pass

fd = FlyingDog()
fd.move()  # Output: Animal moves
fd.bark()  # Output: Woof!
fd.fly()   # Output: Bird flies

Build docs developers (and LLMs) love