I am an SEO Executive and Content Writer at MindStick Software Pvt. Ltd., where I specialize in creating optimized content, improving website visibility, and driving organic growth through strategic SEO.
Inheritance is one of the main principles of Object-Oriented Programming (OOP). It allows a class (called the child or subclass) to
inherit attributes and methods from another class (called the
parent or base class).
This helps you reuse code, extend functionality, and
organize your program better.
Basic Example
# Parent Class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some generic sound"
# Child Class
class Dog(Animal):
def speak(self):
return "Woof!"
# Another Child Class
class Cat(Animal):
def speak(self):
return "Meow!"
# Using the classes
dog = Dog("Buddy")
cat = Cat("Kitty")
print(dog.name, "says:", dog.speak()) # Buddy says: Woof!
print(cat.name, "says:", cat.speak()) # Kitty says: Meow!
Explanation:
Animal is the parent class.
Dog and Cat are child classes that inherit from
Animal.
They override the speak() method to provide their own behavior.
Syntax of Inheritance
class ChildClass(ParentClass):
# child class body
pass
If you don’t specify a parent, the class inherits from Python’s built-in
object by default.
Types of Inheritance in Python
Single Inheritance → One child inherits from one parent.
class A:
pass
class B(A):
pass
Multiple Inheritance → One child inherits from multiple parents.
class A:
pass
class B:
pass
class C(A, B):
pass
Multilevel Inheritance → A class inherits from a child class.
class A:
pass
class B(A):
pass
class C(B):
pass
Hierarchical Inheritance → Multiple children inherit from the same parent.
class Parent:
pass
class Child1(Parent):
pass
class Child2(Parent):
pass
Hybrid Inheritance → A combination of multiple inheritance types.
super() Function
You can use super() to call methods or constructors from the parent class.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call parent constructor
self.breed = breed
dog = Dog("Rex", "Labrador")
print(dog.name, dog.breed)
Method Resolution Order (MRO)
In multiple inheritance, Python uses MRO (Method Resolution Order) to decide which parent class’s method to call first.
You can check it using:
print(ClassName.__mro__)
Advantages of Inheritance
Promotes code reuse
Makes maintenance easier
Enables extensibility (easy to add new features)
Supports polymorphism (same interface, different behavior)
When Not to Use Inheritance
When classes don’t share a logical relationship
When composition (having objects as members) is a better fit
Example:
# Instead of inheritance
class Engine:
pass
class Car:
def __init__(self):
self.engine = Engine() # Composition (has-a relationship)
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
This helps you reuse code, extend functionality, and organize your program better.
Basic Example
Explanation:
Animalis the parent class.DogandCatare child classes that inherit fromAnimal.speak()method to provide their own behavior.Syntax of Inheritance
If you don’t specify a parent, the class inherits from Python’s built-in
objectby default.Types of Inheritance in Python
Single Inheritance
→ One child inherits from one parent.
Multiple Inheritance
→ One child inherits from multiple parents.
Multilevel Inheritance
→ A class inherits from a child class.
Hierarchical Inheritance
→ Multiple children inherit from the same parent.
Hybrid Inheritance
→ A combination of multiple inheritance types.
super()FunctionYou can use
super()to call methods or constructors from the parent class.Method Resolution Order (MRO)
In multiple inheritance, Python uses MRO (Method Resolution Order) to decide which parent class’s method to call first.
You can check it using:
Advantages of Inheritance
When Not to Use Inheritance
Example: