Inheritance in Python (original) (raw)

Last Updated : 5 Jun, 2026

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).

Here, a parent class Animal is created that has a method info(). Then a child class Dog is created that inherits from Animal and adds additional behavior.

Python `

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")

Inherited method

d.info()
d.sound()

`

Output

Animal name: Buddy Buddy barks

**Explanation:

animal_class

Inheritance in Python

Benefits Of Inheritance

super() Function

super() function is used to call methods from a superclass following Python’s Method Resolution Order (MRO). 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

Python `

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): # Calls constructor based on MRO super().init(name)
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:

Method Overriding in Inheritance

Method overriding allows a child class to provide its own implementation of a method that already exists in the parent class. This enables customized behavior while still maintaining the inheritance relationship.

Python `

class Animal: def sound(self): print("Animal sound")

class Dog(Animal): def sound(self): print("Bark")

d = Dog() d.sound()

`

**Explanation: