Last modified: Dec 05, 2026
Python Inheritance and Polymorphism
Inheritance and polymorphism are the two ideas that make Python classes actually useful. One lets you reuse code. The other lets you swap objects without rewriting logic. Together, they shape almost every real OOP design you'll meet.
This article walks through both concepts, how they connect, and where people usually trip up. Expect working code, not theory.
What Inheritance Really Does
A child class inherits attributes and methods from a parent class. That's it. No magic, no hidden rewriting. You define behavior once and extend it where needed.
Here's a small example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
def speak(self):
return f"{self.name} barks" # overrides parent method
dog = Dog("Rex")
print(dog.speak())
Rex barks
Dog gets __init__ for free and replaces speak. That replacement is called method overriding, and it's the bridge to polymorphism.
If you're new to classes in general, start with Python class basics for beginners before going deeper here.
Inheritance Types You'll Actually Use
Python supports several inheritance styles, but most code uses just two or three. Single inheritance is the default. Multiple inheritance shows up in mixins. Multilevel appears when you build layered abstractions.
- Single — one parent, one child. Clean and predictable.
- Multilevel — a chain like A → B → C.
- Multiple — a class with two or more parents.
- Hierarchical — several children from one parent.
For a broader breakdown, the types of Python inheritance guide covers each one with examples. If you want the basics first, inheritance in Python explained is a good starting point.
My advice: stick to single inheritance unless you have a real reason. Multiple inheritance is powerful, but it makes debugging harder. The super() call resolves methods along the MRO, and that order isn't always obvious.
Polymorphism: Same Call, Different Behavior
Polymorphism means one interface, many implementations. You call speak() on any animal object and get the right result. The caller doesn't care which class it's talking to.
class Cat(Animal):
def speak(self):
return f"{self.name} meows"
class Cow(Animal):
def speak(self):
return f"{self.name} moos"
animals = [Dog("Rex"), Cat("Milo"), Cow("Bella")]
for animal in animals:
print(animal.speak())
Rex barks
Milo meows
Bella moos
Notice the loop. It never checks types. That's the whole point. Python doesn't require a shared base class for this to work — it just calls the method and trusts the object to handle it. That's duck typing, and it's why Python polymorphism feels lighter than in Java or C#.
The Python polymorphism types article covers the formal categories if you want the full picture.
Method Overriding vs. Overloading
Python supports overriding, not overloading. You can't define two methods with the same name and different signatures. The second one wins.
If you need optional arguments, use default parameters or *args. Don't try to fake overloading with type checks. It gets messy fast.
class Shape:
def area(self):
raise NotImplementedError("Subclasses must implement area()")
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r ** 2
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side ** 2
for shape in (Circle(2), Square(3)):
print(shape.area())
12.56
9
Raising NotImplementedError in the base class is a simple contract. It tells future readers: override this or the code breaks. Some people prefer the abc module, but this works fine for small projects.
Using super() Correctly
super() calls the parent implementation. Use it when you want to extend behavior, not replace it.
class Puppy(Dog):
def speak(self):
base = super().speak() # calls Dog.speak
return base + " loudly"
print(Puppy("Max").speak())
Max barks loudly
One common bug: forgetting to call super().__init__() in a child class. The parent's setup never runs, and you get missing attributes later. It's an easy fix, but the error message rarely points at the cause.
For more on dunder methods like __init__ and __str__, check the Python dunder methods list.
Polymorphism Without Inheritance
You don't always need a parent class. Two unrelated classes with the same method name work just as well.
class FileLogger:
def log(self, msg):
print(f"[file] {msg}")
class ConsoleLogger:
def log(self, msg):
print(f"[console] {msg}")
def write_log(logger, msg):
logger.log(msg) # no type check needed
write_log(FileLogger(), "saved")
write_log(ConsoleLogger(), "saved")
[file] saved
[console] saved
That's polymorphism in its purest form. No inheritance tree, no abstract base class. Just an agreed method name. Some developers overthink this and build deep hierarchies when a simple shared method would do.
If you want more patterns like this, the Python polymorphism examples page has plenty.
Common Mistakes to Avoid
Three things bite beginners most often:
- Forgetting
super().__init__()in the child constructor. - Overriding a method but changing its expected return type.
- Building inheritance chains deeper than two or three levels.
That last one isn't a rule, but it's a good default. Deep hierarchies are hard to trace. If you find yourself three parents deep, consider composition instead.
Also, don't override methods just to add a print statement. Use decorators or logging wrappers. Overriding is for behavior changes, not debugging.
When to Use Which
Use inheritance when there's a real "is-a" relationship. A Dog is an Animal. A Car is not an Engine — that's composition.
Use polymorphism whenever you have a collection of objects that respond to the same call. It removes if isinstance(...) chains and keeps code open to extension.
Here's the thing: most real bugs come from forcing inheritance where it doesn't belong. If two classes share behavior but aren't related conceptually, a shared interface or duck typing is cleaner.
For a deeper walkthrough of class-level polymorphism, the Python class polymorphism guide is worth a read.
Wrapping Up
Inheritance gives you code reuse. Polymorphism gives you flexibility. Used together, they let you write classes that extend cleanly without touching existing code.
Start simple. One parent, one child, one overridden method. Add complexity only when the design demands it. And remember: Python's duck typing means you often don't need inheritance at all — just a matching method name.
That's the practical core. Everything else is detail.