Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class).
Example: Here, a parent class Animal is created that has a method info(). Then a child classes Dog is created that inherit from Animal and add their own behavior.
class Animal:
def __init__(self, name):
self.name = name
def info(self):
print("Animal name:", self.name)
class Dog(Animal):
def sound(self):
print(self.name, "barks")
d = Dog("Buddy")
d.info() # Inherited method
d.sound()
Output
Animal name: Buddy Buddy barks
Explanation:
- class Animal: Defines the parent class.
- info(): Prints the name of the animal.
- class Dog(Animal): Defines Dog as a child of Animal class.
- d.info(): Calls parent method info() and d.sound(): Calls child method.

Why do we need Inheritance
- Promotes code reusability by sharing attributes and methods across classes.
- Models real-world hierarchies like Animal -> Dog or Person -> Employee.
- Simplifies maintenance through centralized updates in parent classes.
- Enables method overriding for customized subclass behavior.
- Supports scalable, extensible design using polymorphism.
super() Function
super() function is used to call the parent class’s methods. In particular, it is commonly used in the child class's __init__() method to initialize inherited attributes. This way, the child class can leverage the functionality of the parent class.
Example: Here, Dog uses super() to call Animal's constructor
# Parent Class: Animal
class Animal:
def __init__(self, name):
self.name = name
def info(self):
print("Animal name:", self.name)
# Child Class: Dog
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call parent constructor
self.breed = breed
def details(self):
print(self.name, "is a", self.breed)
d = Dog("Buddy", "Golden Retriever")
d.info() # Parent method
d.details() # Child method
Output
Animal name: Buddy Buddy is a Golden Retriever
Explanation:
- The super() function is used inside __init__() method of Dog to call the constructor of Animal and initialize inherited attribute (name).
- This ensures that parent class functionality is reused without needing to rewrite the code in the child class.