Write a program to demonstrate inheritance in python
Here you will get simple example codes to demonstrate inheritance in Python using a base class Parent and a derived class Child.
What is python inheritance ?
Inheritance allows a class (called the derived or child class) to inherit attributes and methods from another class (called the base or parent class).
Types of Python Inheritance
There are five types of inheritance available, that can be implemented in Python:
Single Inheritance
A derived class inherits from a single base class.
Program to demonstrate Single inheritance
1 2 3 4 5 6 7 8 9 10 11 | class Parent: def display(self): print("This is the parent class.") class Child(Parent): def show(self): print("This is the child class.") obj = Child() obj.display() obj.show() |
Output
This is the parent class.
This is the child class.
Multiple Inheritance
A derived class inherits from more than one base class.
Program to demonstrate Multiple inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Parent1: def display1(self): print("This is the first parent class.") class Parent2: def display2(self): print("This is the second parent class.") class Child(Parent1, Parent2): def show(self): print("This is the child class.") obj = Child() obj.display1() obj.display2() obj.show() |
Output
This is the first parent class.
This is the second parent class.
This is the child class.
Multilevel Inheritance
A derived class inherits from another derived class, creating a chain of inheritance.
Program to demonstrate Multilevel inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Grandparent: def display(self): print("This is the grandparent class.") class Parent(Grandparent): def show(self): print("This is the parent class.") class Child(Parent): def reveal(self): print("This is the child class.") obj = Child() obj.display() obj.show() obj.reveal() |
Output
This is the grandparent class.
This is the parent class.
This is the child class.
Hierarchical Inheritance
Multiple derived classes inherit from a single base class.
Program to demonstrate Multilevel inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Parent: def display(self): print("This is the parent class.") class Child1(Parent): def show1(self): print("This is the first child class.") class Child2(Parent): def show2(self): print("This is the second child class.") obj1 = Child1() obj2 = Child2() obj1.display() obj1.show1() obj2.display() obj2.show2() |
Output
This is the parent class.
This is the first child class.
This is the parent class.
This is the second child class.
Hybrid Inheritance
A combination of two or more types of inheritance. It may involve multiple, hierarchical, and multilevel inheritance.
Program to demonstrate Hybrid inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Base: def display(self): print("This is the base class.") class Parent1(Base): def show1(self): print("This is the first parent class.") class Parent2(Base): def show2(self): print("This is the second parent class.") class Child(Parent1, Parent2): def reveal(self): print("This is the child class.") obj = Child() obj.display() obj.show1() obj.show2() obj.reveal() |
Output
This is the base class.
This is the first parent class.
This is the second parent class.
This is the child class.
These above different types of inheritance allow various designs in object-oriented programming, for enabling the reuse of code and creating complex relationships between classes.
Check out our other Python programming examples