Inheritance lets a new class (child) reuse and extend the code of an existing class (parent). The child gets all the parent’s attributes and methods and can add or change them.
What You Will Learn
- Single inheritance
- super() to call the parent
- Method overriding
- isinstance and issubclass
- Multiple inheritance and the MRO
Prerequisites
Single Inheritance and super()
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def details(self):
return f"{self.name} earns {self.salary}"
class Manager(Employee):
def __init__(self, name, salary, team_size):
super().__init__(name, salary) # reuse parent setup
self.team_size = team_size
def details(self): # method overriding
return super().details() + f" and manages {self.team_size} people"
e = Employee("Aman", 40000)
m = Manager("Riya", 90000, 6)
print(e.details())
print(m.details())
print(isinstance(m, Employee), issubclass(Manager, Employee))
Aman earns 40000 Riya earns 90000 and manages 6 people True True
Explanation: Manager(Employee) inherits everything from Employee. super().__init__ runs the parent’s constructor so we do not repeat code. Overriding details() changes behaviour, and super().details() still reuses the parent’s version. A Manager is an Employee, so isinstance is True.
Multiple Inheritance and MRO
class Flyer:
def move(self): return "flying"
class Swimmer:
def move(self): return "swimming"
class Duck(Flyer, Swimmer):
pass
print(Duck().move())
print([c.__name__ for c in Duck.__mro__])
flying ['Duck', 'Flyer', 'Swimmer', 'object']
Explanation: When several parents have the same method, Python follows the Method Resolution Order (MRO): left to right, so Flyer wins. Every class ultimately inherits from object.
Types of Inheritance
| Type | Example |
|---|---|
| Single | Manager(Employee) |
| Multilevel | SeniorManager(Manager) |
| Hierarchical | Manager(Employee), Intern(Employee) |
| Multiple | Duck(Flyer, Swimmer) |
Inheritance vs Composition
Use inheritance for a true “is-a” relationship (a Manager is an Employee). Use composition – an object holding another – for “has-a” (a Car has an Engine). Composition is often simpler and more flexible.
Series
Python series: What is Python · Installation · Syntax · Variables · Operators · Decisions · Loops · Strings · Lists · Dictionary · Functions · Lambda · Modules · Files · Exceptions · OOP · Classes & Objects · Inheritance · Decorators · Generators · Virtual Environments
Interview Questions
Q: What does super() do?
It returns a proxy to the parent class so you can call its methods, such as __init__.
Q: What is the MRO?
The order Python searches classes for a method; view it with ClassName.__mro__.
Practice Exercises
- Create a Vehicle class and Car and Bike subclasses that override a describe() method.
- Add an Intern class with a stipend instead of salary.
Summary
Inheritance reuses a parent class; super() calls the parent, overriding changes behaviour, and the MRO decides which method runs in multiple inheritance.
Next: Decorators
Machine Learning Tutorials, Courses and Certifications