# Inheritance, Polymorphism & super()

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

Subject: Python | Lesson 14 of 24 | 7 min read
Interactive lesson: https://codewithanimation.in/python/inheritance
Previous: [Classes, Instances & the self Parameter](https://codewithanimation.in/python/classes-objects.md) | Next: [Magic (Dunder) Methods & Protocols](https://codewithanimation.in/python/magic-methods.md)

## 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`.

## Common mistake: Forgetting super().__init__()

The most common inheritance defect in Python is overriding `__init__` in a child class without calling `super().__init__(...)`.

If the child class defines its own `__init__`, Python does not run the parent class's `__init__` automatically. Any attributes configured by the parent remain completely uninitialized.

Later, when code calls an inherited parent method that relies on those attributes, Python crashes with an `AttributeError`. Always call `super().__init__(...)` inside the child class initializer unless you intentionally want to bypass parent initialization.

## Tip: Prefer Composition Over Deep Inheritance Trees

Deep inheritance hierarchies (more than two or three levels) become rigid, difficult to refactor, and hard to reason about. Whenever possible, favor object composition ('has-a') over inheritance ('is-a'): pass specialized helper objects into initializers rather than subclassing extensively.

## Try it: 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.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/python/inheritance

## Check yourself

### Question 1

What happens if a child class defines its own `__init__` without calling `super().__init__()`?

- A. The parent __init__ is not called, leaving parent attributes uninitialized
- B. Python raises a SyntaxError at compile time
- C. Python calls the parent __init__ automatically in the background
- D. All child class attributes are erased

Correct answer: A

In Python, overriding `__init__` completely replaces the parent initializer. You must call `super().__init__(...)` explicitly to run the base initialization.

- Why not B: Python permits overriding __init__ without super() syntactically, but it often leads to runtime bugs.
- Why not C: Unlike some other languages, Python does not automatically invoke the base class initializer.
- Why not D: Child attributes are initialized normally, but parent attributes will be missing.

### Question 2

How can you view the exact method resolution search order for a class named `Vehicle`?

- A. Inspect `Vehicle.__mro__` or call `Vehicle.mro()`
- B. Call `inspect.get_parents(Vehicle)`
- C. Inspect `Vehicle.__hierarchy__`
- D. Query `sys.class_path(Vehicle)`

Correct answer: A

Every Python class exposes its linearized inheritance order via the `__mro__` tuple attribute or the `mro()` method.

- Why not B: The standard attribute on the class object is __mro__ or the .mro() method.
- Why not C: __hierarchy__ is not a Python dunder attribute.
- Why not D: sys has no class_path function.

### Question 3

What is the ultimate root base class from which all Python 3 classes inherit?

- A. object
- B. BaseClass
- C. type
- D. Root

Correct answer: A

In Python 3, all classes inherit from `object` by default, ensuring uniform object protocols across the entire language.

- Why not B: BaseClass is not a built-in Python root.
- Why not C: type is the metaclass of classes, but object is the root of the class hierarchy.
- Why not D: Root is not a built-in Python class.

## 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).
