🧬 Inheritance

Last Updated: 06 Sept 2025


Inheritance allows a class (child) to use the properties & methods of another class (parent).

  • Parent Class (Base Class / Superclass) → The one being inherited.
  • Child Class (Derived Class / Subclass) → The one that inherits.
  • isinstance() function → To check if an object is an instance of a class.
  • issubclass() function → To check if a class is a subclass of another class.

Hinglish Tip 🗣: Socho ek "Parent Class" = Animal, aur "Child Class" = Dog. Dog automatically inherits features of Animal (like breathing, eating).


✏ Types of Inheritance in Python

  1. Single Inheritance (most common)
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance

🧬 1. Single Inheritance

It mean There is only one parent class and one child class.

class Animal:
    def breathe(self):
        print("Breathing...")

class Dog(Animal):
    def bark(self):
        print("Barking...")
d1=Dog()
d1.bark()    # Barking...
d1.breathe() # Breathing...
print(isinstance(d1, Animal))   # True
print(issubclass(Dog, Animal))  # True

🧬 2. Multilevel Inheritance

When there is Grandparent → Parent → Child relationship.

class Grandparent:
    def property(self):
        print("I have a house")

class Parent(Grandparent):
    def car(self):
        print("I have a car")

class Child(Parent):
    def bike(self):
        print("I have a bike")

c = Child()
c.property()
c.car()
c.bike()
print(issubclass(Child, Grandparent))  # True
print(issubclass(Child, Parent))       # True
print(issubclass(Parent, Grandparent)) # False

🧬 3. Multiple Inheritance

When a child class inherits from multiple parent classes.

class Father:
    def skill(self):
        print("I know driving")

class Mother:
    def hobby(self):
        print("I love cooking")

class Child(Father, Mother):
    pass

c = Child()
c.skill()   # from Father
c.hobby()   # from Mother

🧬 4. Hierarchical Inheritance

When multiple child classes inherit from a single parent class.

class Parent:
    def house(self):
        print("I own a house")

class Son(Parent):
    def play(self):
        print("I love cricket")

class Daughter(Parent):
    def study(self):
        print("I love reading")

s = Son()
d = Daughter()

s.house()
d.house()

💡 Quick Practice