Last modified: Dec 01, 2026
Python Inheritance Made Simple
Inheritance is one of the core ideas in object-oriented programming. It lets one class reuse the code of another class. This saves time and keeps your code clean.
In Python, inheritance is easy to use. You define a parent class and a child class. The child gets everything the parent has. It can also add or change behavior.
Before you dive in, it helps to know the basics of building classes. If you are new to this, start with these Python class basics for beginners. They make the rest of this article much easier to follow.
What Is Inheritance?
Inheritance is a relationship between two classes. The parent class is also called the base class. The child class is also called the subclass.
The child class inherits the methods and attributes of the parent. You write the shared logic once. Then many child classes can use it.
This is a big win for large projects. You avoid repeating the same code in many places.
Your First Inheritance Example
Let's start with a simple example. We create an Animal class. Then we create a Dog class that inherits from it.
class Animal:
def __init__(self, name):
self.name = name # store the animal name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal): # Dog inherits from Animal
def fetch(self):
return f"{self.name} fetches the ball."
dog = Dog("Rex")
print(dog.speak()) # inherited method
print(dog.fetch()) # own method
Rex makes a sound.
Rex fetches the ball.
Notice that Dog never defines __init__ or speak. It gets them from Animal. That is inheritance in action.
You can see the inherited __init__ method works like a normal constructor. This is closely related to how a Python class with arguments passes values at creation time.
Method Overriding
Sometimes a child class needs its own version of a method. You simply define a method with the same name. This is called overriding.
The child version replaces the parent version for that class. Other children keep the original.
class Animal:
def speak(self):
return "Some generic sound"
class Cat(Animal):
def speak(self): # override the parent method
return "Meow"
class Cow(Animal):
pass # uses the parent version
print(Cat().speak())
print(Cow().speak())
Meow
Some generic sound
Overriding is powerful. It lets each child class define its own behavior while still sharing the rest of the parent code.
Using super() to Call the Parent
Often the child needs the parent logic and some extra logic. The super function lets you call the parent method from inside the child.
This is very common with __init__. The child sets up its own values and lets the parent handle the shared ones.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # call parent constructor
self.breed = breed
def speak(self):
base = super().speak() # call parent method
return f"{base} It is a {self.breed}."
dog = Dog("Rex", "Labrador")
print(dog.speak())
Rex makes a sound. It is a Labrador.
Using super keeps your code flexible. If the parent changes, the child still benefits from that change.
Inheritance and Attributes
Child classes can access attributes created by the parent. They can also add new attributes of their own.
Managing attributes well matters. If you want to control access and add validation, look at the Python property vs attribute clear guide. It explains when to use each one.
Here is a quick example of adding an attribute in the child.
class Vehicle:
def __init__(self, wheels):
self.wheels = wheels
class Car(Vehicle):
def __init__(self, wheels, brand):
super().__init__(wheels)
self.brand = brand
car = Car(4, "Toyota")
print(car.wheels, car.brand)
4 Toyota
Attributes from the parent and child live on the same object. This makes objects rich and easy to work with.
Multiple Inheritance
Python allows a class to inherit from more than one parent. This is called multiple inheritance.
It can be useful, but use it with care. Complex hierarchies can become hard to read.
class Swimmer:
def swim(self):
return "I can swim."
class Flyer:
def fly(self):
return "I can fly."
class Duck(Swimmer, Flyer): # inherits from two classes
pass
duck = Duck()
print(duck.swim())
print(duck.fly())
I can swim.
I can fly.
Python uses a method resolution order to decide which parent method to use. For simple cases, the order in the class definition matters.
Checking Inheritance
Python gives you a few tools to check relationships between classes. The isinstance function checks if an object belongs to a class or its parents.
The issubclass function checks if one class inherits from another.
class Animal:
pass
class Dog(Animal):
pass
dog = Dog()
print(isinstance(dog, Dog))
print(isinstance(dog, Animal))
print(issubclass(Dog, Animal))
True
True
True
These checks are handy when you write functions that accept many types. They let you branch based on the object type.
Dunder Methods and Inheritance
Special methods, often called dunder methods, also follow inheritance rules. If a child does not define one, Python looks up the parent.
This means your custom classes can inherit behavior like printing and comparisons. To learn more, browse this Python dunder methods list. It shows the most useful ones with examples.
Here is a small example where the child inherits a dunder method.
class Base:
def __str__(self):
return "Base object"
class Child(Base):
pass
print(Child())
Base object
The child has no __str__ method. It uses the parent version without any extra code.
Best Practices for Inheritance
Keep your hierarchy shallow. Deep trees are hard to follow and debug.
Use inheritance only when there is a real "is a" relationship. A dog is an animal. That fits. A car is not an engine, even if it has one. Use composition there instead.
Prefer super over calling the parent class by name. It makes your code more maintainable.
Keep parent classes focused. A parent that does too much makes every child heavy.
Common Mistakes
Beginners often forget to call super().__init__ in the child. The shared attributes then never get set.
Another common issue is overriding a method by accident. Check your method names carefully.
Finally, avoid long multiple inheritance chains. They cause confusion about which method actually runs.
Conclusion
Python inheritance is a simple but powerful tool. It lets child classes reuse parent code. It supports overriding, super, and even multiple parents.
Start with small examples. Build a parent class with shared logic. Then add child classes that extend it. Over time, you will see how much cleaner your code becomes.
Practice with the examples above. Change them, break them, and fix them. That is the fastest way to master inheritance in Python.