Inheritance in Python: A Complete Beginner’s Guide
Inheritance is one of the most powerful features of object-oriented programming in Python. It allows you to reuse code across classes and build relationships between them, making your code more organized and efficient.
In this tutorial, you’ll learn everything about inheritance in Python — including its types, use cases, and examples. This guide is designed for beginners and uses simple language and working code examples.
What is inheritance in python?
Inheritance is a feature in object-oriented programming (OOP) that allows a class (child class) to inherit properties and behaviors (methods and attributes) from another class (parent class).
It helps in:
- Reusing existing code
- Establishing logical relationships between classes
- Making code more modular and maintainable
Syntax of inheritance
Here’s the basic syntax to define inheritance in Python:
class Parent:
# parent class code
class Child(Parent):
# child class code
The Child
class inherits all attributes and methods of the Parent
class.
Benefits of using inheritance
- Code reuse: Write once, use in multiple classes
- Hierarchy modeling: Real-world relationships can be represented
- Maintenance: Easier to update code in one place
- Extensibility: Add or override features in child classes
Types of inheritance in python
Python supports multiple types of inheritance. Understanding these types is essential to writing scalable object-oriented code.
1. Single inheritance
In single inheritance, a class inherits from one parent class.
Example:
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.sound()
d.bark()
2. Multilevel inheritance
In multilevel inheritance, a class inherits from a class which itself is a child of another class.
Example:
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
class Puppy(Dog):
def weep(self):
print("Weeping")
p = Puppy()
p.eat()
p.bark()
p.weep()
3. Multiple inheritance
In multiple inheritance, a class inherits from more than one parent class.
Example:
class Father:
def skill1(self):
print("Gardening")
class Mother:
def skill2(self):
print("Cooking")
class Child(Father, Mother):
def skill3(self):
print("Painting")
c = Child()
c.skill1()
c.skill2()
c.skill3()
4. Hierarchical inheritance
In hierarchical inheritance, multiple child classes inherit from a single parent class.
Example:
class Animal:
def breathe(self):
print("Breathing")
class Fish(Animal):
def swim(self):
print("Swimming")
class Bird(Animal):
def fly(self):
print("Flying")
f = Fish()
f.breathe()
f.swim()
b = Bird()
b.breathe()
b.fly()
5. Hybrid inheritance
Hybrid inheritance is a combination of two or more types of inheritance.
Example:
class Person:
def speak(self):
print("Person speaks")
class Father(Person):
def work(self):
print("Father works")
class Mother(Person):
def cook(self):
print("Mother cooks")
class Child(Father, Mother):
def play(self):
print("Child plays")
c = Child()
c.speak()
c.work()
c.cook()
c.play()
Method resolution order (mro)
In multiple inheritance, Python uses a rule called Method Resolution Order (MRO) to decide which parent class’s method is used when multiple parents define the same method.
You can check the MRO using the following code:
print(Child.__mro__)
It returns a tuple showing the order in which Python looks for methods.
Overriding Methods in child class
A child class can override a method from the parent class if needed.
Example:
class Animal:
def sound(self):
print("Generic animal sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
d = Dog()
d.sound() # Output: Dog barks
Using super() to call parent methods
You can use super()
to access methods of the parent class from the child class.
Example:
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
super().sound()
print("Dog barks")
d = Dog()
d.sound()
summary of inheritance types
inheritance type | description |
---|---|
single inheritance | One child class inherits one parent class |
multilevel inheritance | Inheritance chain involving multiple levels |
multiple inheritance | One child class inherits multiple parents |
hierarchical | Multiple child classes from one parent |
hybrid inheritance | Mix of multiple types of inheritance |
Conclusion
Inheritance in Python helps you write clean, reusable, and organized code. By mastering different types of inheritance, you can structure your Python programs more effectively and follow best practices in object-oriented design.
Whether you’re building small projects or large systems, using inheritance wisely can significantly reduce code duplication and enhance functionality.
Faq about Python Inheritance
What is the difference between inheritance and composition?
Inheritance is an “is-a” relationship, while composition is a “has-a” relationship.
Can a class inherit from multiple classes in python?
Yes, Python supports multiple inheritance.
How to override a method in a child class?
Simply redefine the method in the child class using the same name.
What is super() in python?super()
is used to call methods of the parent class from the child class.