Types of Inheritance
Understand the different forms inheritance can take and when to use each.
Introduction
Inheritance comes in several forms. Understanding each type helps you design better class hierarchies and choose the right structure for your program.
Single Inheritance
One parent, one child. This is the most common and simplest form. Everything we have seen so far has been single inheritance.
class Animal:
def speak(self):
print("Some sound")
class Dog(Animal): # single inheritance
def speak(self):
print("Woof!")
Multilevel Inheritance
A chain of inheritance: Grandparent to Parent to Child. Each level adds more specialization. Like Animal to Dog to GuideDog.
class Animal:
def breathe(self):
print("Breathing")
class Dog(Animal):
def bark(self):
print("Woof!")
class GuideDog(Dog):
def guide(self):
print("Guiding owner")
g = GuideDog()
g.breathe() # from Animal
g.bark() # from Dog
g.guide() # own method
The GuideDog has access to methods from every level in the chain.
Hierarchical Inheritance
One parent, multiple children. Several classes inherit from the same base class. Like Animal being the parent of Dog, Cat, and Bird.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def speak(self):
print(f"{self.name}: Woof!")
class Cat(Animal):
def speak(self):
print(f"{self.name}: Meow!")
class Bird(Animal):
def speak(self):
print(f"{self.name}: Tweet!")
Dog("Rex").speak() # Rex: Woof!
Cat("Luna").speak() # Luna: Meow!
Bird("Sky").speak() # Sky: Tweet!
Multiple Inheritance (Preview)
One child inherits from multiple parents. For example, a FlyingFish could inherit from both Fish and Bird. This is powerful but can be tricky.
Note: Multiple inheritance is covered in detail in Week 6. For now, just know it exists and use it carefully.
Comparison
| Type | Structure | Example | Use When |
|---|---|---|---|
| Single | A to B | Animal to Dog | Most situations |
| Multilevel | A to B to C | Animal to Dog to GuideDog | Deep specialization |
| Hierarchical | A to B, A to C | Animal to Dog, Cat, Bird | Sibling classes |
| Multiple | A+B to C | Fish+Bird to FlyingFish | Use carefully |
Best Practices
- Start with single inheritance -- it solves most problems.
- Keep multilevel chains short (2-3 levels). Deep chains are hard to debug.
- Hierarchical inheritance is great when multiple classes share a common base.
Common Mistakes
- Creating too-deep multilevel chains -- 5+ levels becomes very hard to maintain.
- Using multiple inheritance without understanding the diamond problem (covered in Week 6).
- Forcing inheritance when a simpler "has-a" (composition) relationship would work better.
Practice Exercises
- Create a multilevel chain:
VehicletoCartoElectricCar. Each level adds one method. Test thatElectricCarcan call all three. - Create hierarchical inheritance:
Shapeas parent, withCircle,Rectangle, andTriangleas children. Each child has its ownarea()method. - Identify which type of inheritance would be best for: (a) a school system with Teacher and Student, (b) a file system with File to TextFile to EncryptedTextFile, (c) a game with Character having Warrior, Mage, and Archer children.
Summary
- Single: One parent to one child -- the most common form.
- Multilevel: A chain of inheritance -- grandparent to parent to child.
- Hierarchical: One parent with multiple children -- siblings sharing a base.
- Multiple: One child with multiple parents -- powerful but use with care.