How Does Inheritance Work in Python?
Learn how inheritance works in Python: subclasses, method overriding, super(), and the MRO, explained with clear examples and interview-ready answers.
Expected Interview Answer
Inheritance lets a class (the child or subclass) reuse the attributes and methods of another class (the parent or superclass), so shared behaviour is written once and specialised where needed.
You declare it with class Child(Parent). The child automatically gains the parent's methods and can override them or add new ones. Python resolves which method to call using the Method Resolution Order (MRO), and super() lets a child call the parent's version of a method, most commonly inside __init__ to run the parent's setup. Python also supports multiple inheritance, where a class derives from several parents at once.
- Reuses code instead of duplicating it
- Models real 'is-a' relationships clearly
- Lets subclasses override or extend behaviour
- Centralises shared logic in one base class
- Enables polymorphism through a common interface
AI Mentor Explanation
A base 'Cricketer' class knows how to bat, field and train. A 'Bowler' subclass inherits all of that for free, then adds its own bowl() action, while a 'WicketKeeper' inherits the same base but overrides catching. Nobody re-teaches fielding to each new player type.
Step-by-Step Explanation
Step 1
Define the base class
Write a parent class with the attributes and methods that several classes will share, e.g. class Animal with a speak() method.
Step 2
Declare the subclass
Create the child with class Dog(Animal): so it automatically inherits every public attribute and method of Animal.
Step 3
Call super() in __init__
Inside the child's __init__, call super().__init__(...) to run the parent's initialisation before adding child-specific attributes.
Step 4
Override where needed
Redefine a method with the same name in the child to specialise behaviour; Python uses the child's version for its instances.
Step 5
Rely on the MRO
When calling a method, Python walks the Method Resolution Order (base.__mro__) to find the first matching definition, which also drives multiple inheritance.
What Interviewer Expects
- Correct syntax class Child(Parent)
- Understanding of method overriding
- Purpose of super() and calling parent __init__
- Awareness of the MRO and multiple inheritance
- Ability to give a concrete is-a example
Common Mistakes
- Forgetting to call super().__init__() so parent setup never runs
- Confusing inheritance (is-a) with composition (has-a)
- Assuming private name-mangled attributes are inherited normally
- Not knowing how the MRO resolves diamond inheritance
- Overusing deep inheritance chains instead of composition
Best Answer (HR Friendly)
“Inheritance is a way to build a new class on top of an existing one so it reuses that class's features automatically. The new class keeps everything from the original and can change or add parts, which saves repeating the same code.”
Code Example
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # run parent setup
self.breed = breed
def speak(self): # override
return f"{self.name} barks"
d = Dog("Rex", "Labrador")
print(d.speak()) # Rex barks
print(d.name, d.breed) # Rex Labrador
print(Dog.__mro__) # (Dog, Animal, object)Follow-up Questions
- What is the Method Resolution Order and how does Python compute it?
- How does multiple inheritance work and what is the diamond problem?
- When should you prefer composition over inheritance?
- What does super() actually do behind the scenes?
- How do abstract base classes fit into inheritance?
MCQ Practice
1. Which syntax makes Dog inherit from Animal?
In Python a subclass lists its parent in parentheses after the class name: class Dog(Animal):.
2. What does super().__init__() typically do in a child class?
super().__init__() calls the parent class's __init__, ensuring the inherited attributes are set up before child-specific ones.
3. What determines which method Python calls in an inheritance chain?
Python searches the MRO (viewable via Class.__mro__) to find the first class that defines the requested method.
Flash Cards
What is a subclass? — A class that inherits attributes and methods from a parent (base) class and can extend or override them.
What does super() do? — It returns a proxy to the parent class so you can call the parent's methods, commonly super().__init__().
What is method overriding? — Redefining a parent's method in the child with the same name so the child's version is used for its instances.
What is the MRO? — Method Resolution Order — the linearised order Python searches classes to resolve a method, seen via Class.__mro__.