Last modified: Feb 10, 2026 By Alexander Williams
Python Object Inheritance Guide for Beginners
Python object inheritance is a core OOP concept. It lets you create new classes based on existing ones. This promotes code reuse and logical structure.
Think of it like a family tree. A child class inherits traits from a parent class. The child can also have its own unique features. This creates a hierarchy.
This guide explains inheritance clearly. We will use simple examples. You will learn how to build and extend classes effectively.
What is a Parent Class?
A parent class is the base class. It defines common attributes and methods. These are the shared traits for a group of related objects.
Let's create a simple parent class called Vehicle. All vehicles have some common properties.
# Parent Class (Base Class)
class Vehicle:
def __init__(self, brand, model):
# These are attributes common to all vehicles
self.brand = brand
self.model = model
def display_info(self):
# A method to show basic info
return f"This vehicle is a {self.brand} {self.model}."
The Vehicle class has an initializer (__init__) and a method (display_info). Any class that inherits from it will get these features automatically.
Creating a Child Class
A child class inherits from a parent. It is also called a derived or subclass. You define it using parentheses.
We will create a Car class that is a child of Vehicle.
# Child Class (Derived Class)
class Car(Vehicle): # Car inherits from Vehicle
def __init__(self, brand, model, doors):
# Call the parent's __init__ method
super().__init__(brand, model)
# Add a new attribute specific to Car
self.doors = doors
# A new method only for Car objects
def honk(self):
return "Beep beep!"
The Car class uses super().__init__(). This calls the parent's initializer. It sets up the brand and model attributes.
The child then adds its own attribute, doors, and a new method, honk. Let's see it in action.
# Create an instance of Car
my_car = Car("Toyota", "Camry", 4)
# Use inherited method
print(my_car.display_info())
# Use child's own method
print(my_car.honk())
# Access inherited and new attributes
print(f"It has {my_car.doors} doors.")
This vehicle is a Toyota Camry.
Beep beep!
It has 4 doors.
The Car object successfully used the inherited display_info method. It also used its own honk method. This is the power of inheritance.
Overriding Parent Methods
Sometimes a child class needs different behavior. You can override a parent method. Just define a method with the same name in the child class.
Let's create another child class, ElectricCar. It will override the display_info method.
class ElectricCar(Car):
def __init__(self, brand, model, doors, battery_size):
super().__init__(brand, model, doors)
self.battery_size = battery_size # in kWh
# Override the parent's display_info method
def display_info(self):
# Get the original info from the parent
original_info = super().display_info()
# Add new electric-specific info
return f"{original_info} It is electric with a {self.battery_size}kWh battery."
Notice how it calls super().display_info(). This gets the string from the parent's method. Then it adds extra information.
my_tesla = ElectricCar("Tesla", "Model 3", 4, 75)
print(my_tesla.display_info())
print(my_tesla.honk()) # Inherited from Car
This vehicle is a Tesla Model 3. It is electric with a 75kWh battery.
Beep beep!
The ElectricCar provided a specialized version of display_info. It still has access to all other inherited methods like honk.
The isinstance() and issubclass() Functions
Python provides built-in functions to check relationships. The isinstance() function checks if an object is an instance of a class. The issubclass() function checks if a class is a subclass of another.
These are useful for type checking and understanding your object hierarchy.
print(isinstance(my_tesla, ElectricCar)) # True
print(isinstance(my_tesla, Car)) # True (because ElectricCar inherits from Car)
print(isinstance(my_tesla, Vehicle)) # True (inheritance chain)
print(issubclass(ElectricCar, Car)) # True
print(issubclass(Car, Vehicle)) # True
print(issubclass(ElectricCar, Vehicle)) # True
True
True
True
True
True
True
Multiple Inheritance
Python supports multiple inheritance. A class can inherit from more than one parent. You list the parent classes in parentheses, separated by commas.
This is powerful but can be complex. You must manage the Method Resolution Order (MRO). This is the order Python searches for methods.
class Engine:
def start(self):
return "Engine roaring to life."
class Radio:
def play_music(self):
return "Playing music."
class ModernCar(Car, Engine, Radio):
def __init__(self, brand, model, doors, feature):
super().__init__(brand, model, doors)
self.feature = feature
my_modern_car = ModernCar("Ford", "Focus", 5, "GPS")
print(my_modern_car.start()) # From Engine class
print(my_modern_car.play_music()) # From Radio class
print(my_modern_car.honk()) # From Car class (via inheritance)
Engine roaring to life.
Playing music.
Beep beep!
The ModernCar class inherited methods from three different parents. Understanding Python objects, classes, instances, and methods is key before tackling multiple inheritance.
When to Use Inheritance
Use inheritance for an "is-a" relationship. A Car is a Vehicle. An ElectricCar is a Car.
Do not use it for a "has-a" relationship. A Car has an Engine. That is better modeled with object composition (objects in objects).
Inheritance creates tight coupling. Changes in the parent can affect all children. Use it carefully for truly hierarchical relationships.
Common Pitfalls and Best Practices
Avoid deep inheritance chains. They become hard to understand and maintain. Favor shallow hierarchies.
Always use super() to call parent methods. This ensures the MRO is followed correctly, especially with multiple inheritance.
Remember that inherited objects might need special handling when converting to formats like JSON. For that, check out our guide on JSON serialization for custom Python objects.
Conclusion
Python object inheritance is a fundamental tool. It helps you write DRY (Don't Repeat Yourself) code. You build classes that share common logic.
You learned about parent and child classes. You saw how to override methods and use super(). We also covered multiple inheritance and key functions like isinstance().
Start by modeling simple "is-a" relationships. Use inheritance to extend functionality cleanly. As your programs grow, these concepts will help you create robust and organized code.
Mastering inheritance is a major step in object-oriented programming. Combine it with other concepts like encapsulation and polymorphism for powerful designs.