Python Inheritance (Simple, Clear & Beginner-Friendly Guide)
Inheritance is one of the core pillars of Object Oriented Programming (OOP). In simple words, inheritance allows one class to use the properties and methods of another class without writing the same code again.
Just like children inherit characteristics from their parents, a Python class (child class) can inherit behavior from another class (parent class). This helps us write cleaner, shorter and more reusable code.
If you’re new to OOP, don’t worry, this guide explains everything in an extremely simple and practical way.
What Is Inheritance?
Inheritance allows a new class to access variables and functions of an existing class. The existing class is called the parent (base) class, and the new one is the child (derived) class.
- Parent class → class whose features are reused
- Child class → class that inherits parent features
Inheritance supports the DRY rule: Don’t Repeat Yourself.
For more basics, read our earlier post on Python Classes and Objects.
Why Use Inheritance?
- To reuse code instead of rewriting it
- To organize programs into structured classes
- To extend existing functionality
- To simplify maintenance
Basic Syntax
class Parent:
# parent class
pass
class Child(Parent):
# inherits everything from Parent
pass
Example 1: Simple Inheritance
Here’s a friendly and fresh example:
class Vehicle:
def start(self):
print("Vehicle is starting...")
class Bike(Vehicle):
def ride(self):
print("Riding the bike!")
my_bike = Bike()
my_bike.start() # inherited
my_bike.ride() # child class method
Output:
Vehicle is starting...
Riding the bike!
Notice how Bike automatically gets the start() method from Vehicle.
Example 2: Inheriting from a Person Class
Step 1: Create Parent Class
class Person:
def __init__(self, first, last):
self.first = first
self.last = last
def full_name(self):
print(f"{self.first} {self.last}")
p = Person("Aarav", "Sharma")
p.full_name()
Output:
Aarav Sharma
Step 2: Create Child Class
class Employee(Person):
pass
emp = Employee("Neha", "Singh")
emp.full_name()
Output:
Neha Singh
Types of Inheritance in Python
- Single Inheritance : one parent, one child
- Multiple Inheritance : child inherits from multiple parents
- Multilevel Inheritance : chain of inheritance
- Hierarchical Inheritance : multiple children share a single parent
- Hybrid Inheritance : a mix of above types
Example 3: Multiple Inheritance
class Writer:
def write(self):
print("Writes articles")
class Designer:
def design(self):
print("Designs graphics")
class Creator(Writer, Designer):
def create(self):
print("Creates content")
c = Creator()
c.write()
c.design()
c.create()
Output:
Writes articles
Designs graphics
Creates content
Using super() : Calling Parent Methods
The super() function lets the child class access parent-class methods even when it overrides them.
class Appliance:
def info(self):
print("This is an electrical appliance.")
class Fan(Appliance):
def info(self):
super().info()
print("This fan has 3 speed levels.")
f = Fan()
f.info()
Output:
This is an electrical appliance.
This fan has 3 speed levels.
ADVANCED TOPIC: Method Overriding
When a child class has a method with the same name as the parent, the child method replaces the parent version.
class Camera:
def shoot(self):
print("Capturing standard photo...")
class DSLR(Camera):
def shoot(self):
print("Capturing high-resolution photo!")
d = DSLR()
d.shoot()
ADVANCED TOPIC: Multilevel Inheritance
class A:
def feature1(self):
print("Feature from A")
class B(A):
def feature2(self):
print("Feature from B")
class C(B):
def feature3(self):
print("Feature from C")
obj = C()
obj.feature1()
obj.feature2()
obj.feature3()
ADVANCED TOPIC: Method Resolution Order (MRO)
In multiple inheritance, Python decides which class to search first using MRO.
print(Creator.mro())
This shows the order Python follows while resolving methods.
Conclusion
Inheritance makes Python code more efficient, manageable, and extendable. By reusing existing classes, you build apps faster and keep your code clean. Combined with super(), method overriding, and MRO, inheritance becomes a powerful tool for both beginners and advanced developers.
For related topics, read: Python Classes and Objects.
FAQs (Python Inheritance)
- Q: Can a child class override parent methods?
Yes. Simply create a method with the same name in the child class. - Q: Does Python support multiple inheritance?
Yes, a class can inherit from more than one parent. - Q: What is
super()used for?
To call the parent class’s method inside the child class. - Q: What happens if both classes contain the same method?
The child class method takes priority. - Q: Is inheritance mandatory in OOP?
No, but it makes code cleaner and reusable.
📢 Call To Action
If this guide helped you understand inheritance clearly, explore more Python tutorials on our blog. Don’t forget to bookmark this page and share it with learners.