- Courses
- 💻Programming Languages
- 🐍Python
- Object‑Oriented Programming (OOP)
- Inheritance
On this page
Inheritance
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
The Purpose of Inheritance
Inheritance allows one class (the child or subclass) to acquire the attributes and methods of another class (the parent or base class). It is a core mechanism in object‑oriented programming that enables:
- Code reuse
- Extension of existing behavior
- Clear hierarchical modeling
- Reduction of duplication
Inheritance lets you build specialized versions of existing classes while preserving shared functionality.
Defining a Subclass
A subclass is defined by placing the parent class in parentheses.
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
pass
Here, Dog inherits everything from Animal.
Calling:
d = Dog()
d.speak()
works because Dog automatically gains the speak method from Animal.
Adding New Behavior in a Subclass
A subclass may define additional attributes or methods.
class Dog(Animal):
def wag_tail(self):
return "Wagging tail"
Dog now has both:
- Inherited behavior (
speak) - New behavior (
wag_tail)
This is the essence of extending a class.
Overriding Methods
A subclass may replace (override) a method inherited from the parent class.
class Dog(Animal):
def speak(self):
return "Bark"
Now:
Dog().speak() # "Bark"
The subclass version takes precedence.
Overriding allows specialization while keeping the overall structure intact.
Calling the Parent Version with super()
Sometimes you want to extend, not replace, the parent behavior.
Use super() to access the parent implementation.
class Animal:
def speak(self):
return "Some sound"
class Cat(Animal):
def speak(self):
base = super().speak()
return base + " (but softer)"
super() ensures proper cooperation in class hierarchies and avoids hard‑coding parent names.
Inheriting and Extending __init__
Subclasses often need their own initialization while still using the parent’s setup.
class Animal:
def __init__(self, species):
self.species = species
class Dog(Animal):
def __init__(self, name):
super().__init__("Dog")
self.name = name
This pattern ensures:
- The parent initializes shared attributes
- The subclass initializes its own attributes
Multi‑Level Inheritance
A class may inherit from a subclass, forming a chain.
class Animal:
pass
class Mammal(Animal):
pass
class Dog(Mammal):
pass
Each level inherits from the one above it.
Python resolves attributes by searching upward through the chain.
Multiple Inheritance
Python allows a class to inherit from more than one parent.
class Flyer:
def fly(self):
return "Flying"
class Swimmer:
def swim(self):
return "Swimming"
class Duck(Flyer, Swimmer):
pass
Duck now has both fly and swim.
Multiple inheritance is powerful but should be used carefully to avoid complexity.
Method Resolution Order (MRO)
When multiple inheritance is used, Python determines which parent to search first using the Method Resolution Order (MRO).
You can inspect it with:
Duck.__mro__
Python uses a consistent algorithm (C3 linearization) to ensure predictable behavior.
Best Practices for Inheritance
- Use inheritance for is‑a relationships (e.g., Dog is a Animal)
- Prefer composition when the relationship is not hierarchical
- Keep class hierarchies shallow and meaningful
- Override methods only when necessary
- Use
super()consistently to maintain cooperative behavior - Avoid unnecessary multiple inheritance
These practices lead to clear, maintainable object‑oriented designs.
Summary
In this lesson, you learned how inheritance enables classes to reuse and extend behavior. You explored subclass definitions, method overriding, super(), multi‑level inheritance, multiple inheritance, and the method resolution order. These concepts form the structural backbone of object‑oriented programming and prepare you for more advanced topics such as polymorphism and abstract classes.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Attributes & Methods
Next
Polymorphism
- The Purpose of Inheritance
- Defining a Subclass
- Adding New Behavior in a Subclass
- Overriding Methods
- Calling the Parent Version with super()
- Inheriting and Extending
__init__ - Multi‑Level Inheritance
- Multiple Inheritance
- Method Resolution Order (MRO)
- Best Practices for Inheritance
- Summary
