Module 11 · Object-Oriented Programming — topic 15 of 25
Inheritance
Creating a class that extends another: the 'is-a' relationship, inheriting attributes and methods, adding new behaviour, super(), isinstance with subclasses, and when composition is better.
Learning objectives
- Create subclasses that reuse parent code
- Extend parent behaviour with super()
- Recognise genuine is-a relationships
Definition
Inheritance lets a new class (the subclass or child) be defined in terms of an existing class (the superclass or parent). The subclass inherits the parent's methods and attributes and can add new ones or override existing ones.
Introduction
A SavingsAccount is an Account that also earns interest. Instead of copying Account's code, the subclass inherits it and adds only what's different.
Why it matters
Inheritance removes duplication across related types and lets code that works with the parent also work with every child — the basis of polymorphism.
Detailed explanation
Syntax: class Child(Parent): ... The child gets all of the parent's methods and class attributes.
Every class implicitly inherits from object, which provides default __str__, __eq__ and other methods.
A subclass can add new methods and attributes, and override parent methods by defining a method with the same name.
super() gives access to the parent's version: super().__init__(...) runs the parent's initialisation, super().describe() reuses its behaviour.
isinstance(obj, Parent) is True for instances of subclasses too; issubclass(Child, Parent) checks the relationship between classes.
Use inheritance only for true 'is-a' relationships. If a class merely uses another (a Car has an Engine), use composition instead.
Syntax
- class Child(Parent):
- super() reaches the parent's methods.
- Subclass instances are also parent instances.
Code examples
Inheriting methods
Bruno is an animal Bruno fetches the ball True True True
Extending with super()
Asha: 12000 @ 4%
More examples
One function, many subclasses
Ravi Employee 40000 Asha Manager 75000 Meera Intern 15000 payroll: 130000
Step-by-step explanation
- Manager adds a team_size attribute and extends monthly_pay with super().
- Intern doesn't define __init__, so it inherits Employee's constructor.
- The loop calls monthly_pay on every object without checking its type; each class supplies its own rule.
Expected output
Running “One function, many subclasses” prints exactly:
Practical applications
- Exception classes form a big inheritance tree.
- Frameworks ask you to subclass their base classes (Django's View, unittest.TestCase).
Best practices
- Keep hierarchies shallow (two or three levels).
- Use super() to extend parent behaviour.
- Prefer composition unless the is-a relationship is genuine.
Common mistakes
Inheritance for code reuse only
Avoid
Prefer
Use composition when the relationship is has-a.
Calling the parent method by name
Avoid
Prefer
super() follows the method resolution order correctly.
Important notes
- The following lessons look at single, multiple and multilevel inheritance in more detail.
Key takeaways
- class Child(Parent) reuses the parent's code.
- Subclasses can add and override methods; super() reaches the parent.
- Inheritance models is-a; composition models has-a.
Try it yourself
- 1Create Vehicle, Car and Bike classes where each subclass overrides wheels() and description().
- 2Add a Contractor subclass to the Employee example with an hourly pay rule.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?