Inheritance in Python
-
Learn how classes inherit properties and methods in Python using different types of inheritance.
Inheritance
Inheritance is an Object-Oriented Programming concept where a child class acquires properties and methods of a parent class.
👉 Simple meaning:
Child class uses parent class featuresWhy Do We Use Inheritance?
Reuse existing code
Reduce code duplication
Improve maintainability
Create parent-child relationships
Support polymorphism
Real-Life Example
Family
Child inherits traits from parents
This is Inheritance
Basic Syntax of Inheritance
class Parent:
pass
class Child(Parent):
pass
Types of Inheritance in Python
Python supports 5 types of inheritance:
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
1.Single Inheritance
One child class inherits from one parent class.
Python Inheritance Syntax
# Parent class (Base class)
class Parent:
pass # pass means no code inside the class
# Child class (Derived class)
# Child inherits properties of Parent
class Child(Parent):
pass # Child currently has no extra features
Single Inheritance Example
The child class inherits properties from the parent class.
class Parent:
def show(self):
print("This is Parent class")
class Child(Parent):
def display(self):
print("This is Child class")
c = Child()
c.show()
c.display()
2.Multiple Inheritance
One child class inherits from multiple parent classes.
Multiple Inheritance Syntax
# First parent class
class Parent1:
pass # No code inside Parent1 for now
# Second parent class
class Parent2:
pass # No code inside Parent2 for now
# Child class inheriting from Parent1 and Parent2
class Child(Parent1, Parent2):
pass # Child can access properties of both Parent1 and Parent2
Multiple Inheritance Example
The child class accesses methods from both parent classes.
def skill1(self):
print("Father: Driving")
class Mother:
def skill2(self):
print("Mother: Cooking")
class Child(Father, Mother):
pass
c = Child()
c.skill1()
c.skill2()
3.Multilevel Inheritance
A class inherits from a child class (Parent → Child → Grandchild).
Multilevel Inheritance Syntax
# GrandParent class (base class)
class GrandParent:
pass # No code inside GrandParent for now
# Parent class inherits from GrandParent
class Parent(GrandParent):
pass # Parent gets properties of GrandParent
# Child class inherits from Parent
class Child(Parent):
pass # Child gets properties of Parent and GrandParent
Multilevel Inheritance Example
Properties pass through multiple levels.
class GrandParent:
def g(self):
print("Grandparent")
class Parent(GrandParent):
def p(self):
print("Parent")
class Child(Parent):
def c(self):
print("Child")
obj = Child()
obj.g()
obj.p()
obj.c()
4.Hierarchical Inheritance
Multiple child classes inherit from one parent class.
Hierarchical Inheritance syntax
# Base (parent) class
class Parent:
pass # Common properties for all child classes
# First child class inheriting from Parent
class Child1(Parent):
pass # Child1 gets properties of Parent
# Second child class inheriting from Parent
class Child2(Parent):
pass # Child2 also gets properties of Parent
Hierarchical Inheritance Example
One parent class shared by multiple child classes.
class Animal:
def eat(self):
print("Animal eats")
class Dog(Animal):
pass
class Cat(Animal):
pass
d = Dog()
c = Cat()
d.eat()
c.eat()
5.Hybrid Inheritance
Combination of two or more inheritance types.
Hybrid Inheritance syntax
# Base class
class GrandParent:
pass # Top-level parent class
# Parent class inherits from GrandParent (Multilevel inheritance)
class Parent1(GrandParent):
pass
# Another parent class
class Parent2:
pass
# Child class inherits from Parent1 and Parent2 (Multiple inheritance)
class Child(Parent1, Parent2):
pass # Combination of multiple + multilevel inheritance
Hybrid Inheritance Example
This example combines multiple and hierarchical inheritance.
class A:
def a(self):
print("Class A")
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
d = D()
d.a()
What is the super() Keyword?
Definition
super() is used to call methods or constructors of the parent class from the child class.
Why Use super()?
Access parent class constructor
Avoid code duplication
Supports multiple inheritance
Improves maintainability
Inheritance Using super()
Child class calls parent constructor using super().
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, roll):
super().__init__(name)
self.roll = roll
def show(self):
print("Name:", self.name)
print("Roll No:", self.roll)
s = Student("Rahul", 101)
s.show()
❌ Inheritance Without super()
Without super(), we must manually call the parent class constructor.
Inheritance Without super()
Parent constructor is called using class name.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, roll):
Person.__init__(self, name) # Without super
self.roll = roll
def show(self):
print("Name:", self.name)
print("Roll No:", self.roll)
s = Student("Amit", 102)
s.show()