Learning navigation

Code with Animation

Inheritance, Polymorphism & super()

Master class inheritance hierarchies in Python: single and multiple inheritance, method overriding, super() delegation, and the C3 Method Resolution Order (MRO).

Python7 min readConcept 14 of 24

Code Reuse Through Class Hierarchies

Inheritance enables a new class (the child or subclass) to acquire the attributes and methods of an existing class (the parent or base class). You declare inheritance by passing the parent class in parentheses: class Dog(Animal):.

The child class automatically inherits all parent methods without duplicating code. In addition, the child class can introduce new methods unique to itself.

Polymorphism allows different child classes to define their own custom versions of the same method name, enabling callers to treat varied subclasses through a single unified interface.

Method Overriding and super() Delegation

When a child class defines a method with the same name as a parent method, it overrides the parent implementation. When called, Python executes the child's specialized version.

Frequently, a child method needs to extend the parent behavior rather than replace it entirely. The super() built-in returns a proxy object that delegates method calls to the parent class in the inheritance chain.

Using super().method(*args) lets you execute the base logic first and then layer subclass-specific behavior on top cleanly.

Method Resolution Order (MRO)

Python supports multiple inheritance, where a class inherits from two or more parent classes: class FlyingCar(Car, Airplane):.

To determine which method to invoke when method names collide across parents, Python follows the C3 Linearization algorithm to build a deterministic Method Resolution Order (MRO).

You can inspect any class's exact lookup sequence by accessing its __mro__ tuple or calling ClassName.mro(). Python always searches classes in that strict left-to-right order, ending at the ultimate root class object.

The Inheritance Tree & MRO Resolution Studio

Step through method invocations across an Animal and Dog class hierarchy. Watch how method lookups resolve locally on the child class or ascend via super() to execute base logic.

Select Inheritance Operation:
# 1. Base parent class:
class Animal:
def __init__(self, name):
self.name = name
def speak(self): return "..."
def sleep(self): return "Zzz"
# 2. Subclass definition:
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
return "Woof!" # Override
# 3. Invocation action:
dog.speak()
# "Woof!" (Subclass match)
INHERITANCE TREE & MRO RESOLUTIONPARENT CLASS: Animalinherits from object__init__(name)speak() → "..."sleep() → "Zzz"CHILD CLASS: Dog(Animal)Direct Subclass__init__(self, name, breed):↳ super().__init__(name)speak(self) [OVERRIDDEN]↳ returns "Woof!"DISPATCH: dog.speak() → Resolved on Subclass (Dog)✓ Subclass override executes immediately, returning 'Woof!'
★ Python Rule: Python checks the subclass first for methods. If absent, it ascends the inheritance chain via the Method Resolution Order (MRO) to find the nearest base implementation. Always call super().__init__() in subclasses to initialize parent state.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What happens if a child class defines its own `__init__` without calling `super().__init__()`?
  2. 2How can you view the exact method resolution search order for a class named `Vehicle`?
  3. 3What is the ultimate root base class from which all Python 3 classes inherit?

Remember this

  • A child class inherits methods and attributes from its parent: class Child(Parent):.
  • Child classes can override parent methods to provide specialized behavior (polymorphism).
  • Use super().method() to delegate to the parent class without hardcoding the parent class name.
  • Forgetting super().__init__() in a subclass leaves parent attributes uninitialized and causes AttributeErrors.
  • Python resolves method collisions in multiple inheritance using the C3 Method Resolution Order (MRO).

Done with this concept?

Mark it complete to track your progress. No login needed.