Python — Inheritance
Reusing a blueprint
A child class inherits everything from its parent and adds/changes what it needs:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal): # Dog EXTENDS Animal
def speak(self): # override
return f"{self.name} says woof!"
class Robot(Animal):
pass # inherit unchanged
Dog("Rex").speak() # "Rex says woof!"
Robot("R2").speak() # "R2 makes a sound"
super() — calling the parent
Child constructors must initialize the parent part:
class Vehicle:
def __init__(self, brand, wheels):
self.brand = brand
self.wheels = wheels
class ElectricCar(Vehicle):
def __init__(self, brand, battery_kwh):
super().__init__(brand, wheels=4) # parent setup first
self.battery_kwh = battery_kwh
def info(self):
parent = super().speak if False else None # super works for methods too
return f"{self.brand}, {self.battery_kwh} kWh"
class Cat(Animal):
def speak(self):
base = super().speak() # extend instead of replace
return base + " (but fancier)"
Forgetting super().__init__() means parent attributes never exist → AttributeError later.
isinstance — checking the family tree
isinstance(Dog("Rex"), Animal) # True — Dogs ARE Animals
issubclass(Dog, Animal) # True
Enables polymorphic code:
for animal in [Dog("Rex"), Cat("Mio"), Robot("R2")]:
print(animal.speak()) # right version chosen automatically
Method overriding rules
Same name = child wins. To extend rather than replace, call super().method() inside. super() follows the MRO (method resolution order) — Python's deterministic search order through multiple parents.
Multiple inheritance exists (carefully)
class Swimmer:
def swim(self): return "swimming"
class Flyer:
def fly(self): return "flying"
class Duck(Swimmer, Flyer):
pass
Duck().swim(); Duck().fly()
Powerful but a source of confusion (diamond problem) — most code stays single-parent; mixins are the accepted use.
Composition over inheritance
When you only NEED another object's behavior, contain it:
class Engine:
def start(self): return "vroom"
class Car:
def __init__(self):
self.engine = Engine() # HAS-A, not IS-A
def start(self): return self.engine.start()
Heuristic: "Dog IS-A Animal" → inheritance · "Car HAS-A Engine" → composition.
Mini Practice
- Shape base + Circle/Square subclasses with their own area().
- Extend BankAccount: SavingsAccount adding interest().
- Call super().speak() inside an overriding method.
- Polymorphism loop printing sounds from mixed instances.
- Refactor an inheritance chain into composition where HAS-A fits better.
Next: iterators →
Related Topics
Frequently Asked Questions about Inheritance
What is Inheritance in Python?
Inheritance is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Inheritance?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Inheritance.
Why is Inheritance important in Python?
Inheritance is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.