Write a program to demonstrate inheritance in python

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.

Single inheritance demonstrate inheritance in python

Program to demonstrate Single inheritance 

Output

This is the parent class.
This is the child class.

 

 

Multiple Inheritance

A derived class inherits from more than one base class.

multiple inheritance demonstrate inheritance in python

Program to demonstrate Multiple inheritance 

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.

multi level inheritance demonstrate inheritance in python

Program to demonstrate Multilevel inheritance 

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.

hiierarchical inheritance demonstrate inheritance in python

Program to demonstrate Multilevel inheritance 

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.

hybrid inheritance demonstrate inheritance in python

Program to demonstrate Hybrid inheritance 

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

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top