0% found this document useful (0 votes)
3 views7 pages

Types of Inheritance

The document outlines five types of inheritance in Python: single, multiple, multilevel, hierarchical, and hybrid inheritance. Each type is explained with examples, demonstrating how child classes can inherit properties from parent classes in various configurations. The document provides code snippets to illustrate the concepts effectively.

Uploaded by

rathodevv6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Types of Inheritance

The document outlines five types of inheritance in Python: single, multiple, multilevel, hierarchical, and hybrid inheritance. Each type is explained with examples, demonstrating how child classes can inherit properties from parent classes in various configurations. The document provides code snippets to illustrate the concepts effectively.

Uploaded by

rathodevv6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python MMN

Types of Inheritance
In Python, based upon the number of child and parent classes involved, there
are five types of inheritance. The types of inheritance are listed below:

1. Single inheritance
2. Multiple Inheritance
3. Multilevel inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

Single Inheritance
In single inheritance, a child class inherits from a single-parent class. Here
is one child class and one parent class.

Parent Class

Child Class

Python Single Inheritance

Example

Let’s create one parent class called Vehicle and one child class called Car to
implement single inheritance.

Mr. D.S.Kiwde Page 1


Python MMN

# Base class
class Vehicle:
def Vehicle_info(self):
print('Inside Vehicle class')

# Child class
class Car(Vehicle):
def car_info(self):
print('Inside Car class')

# Create object of Car


car = Car()

# access Vehicle's info using car object


car.Vehicle_info()
car.car_info()

Output

Inside Vehicle class

Inside Car class

Multiple Inheritance
In multiple inheritance, one child class can inherit from multiple parent
classes. So here is one child class and multiple parent classes.

Mr. D.S.Kiwde Page 2


Python MMN

Example

# Parent class 1
class Person:
def person_info(self, name, age):
print('Inside Person class')
print('Name:', name, 'Age:', age)

# Parent class 2
class Company:
def company_info(self, company_name, location):
print('Inside Company class')
print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):
def Employee_info(self, salary, skill):
print('Inside Employee class')
print('Salary:', salary, 'Skill:', skill)

# Create object of Employee


emp = Employee()

# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.Employee_info(12000, 'Machine Learning')

Output

Inside Person class

Name: Jessa Age: 28

Inside Company class

Name: Google location: Atlanta

Inside Employee class

Salary: 12000 Skill: Machine Learning

Mr. D.S.Kiwde Page 3


Python MMN

In the above example, we created two parent


classes Person and Company respectively. Then we create one child
called Employee which inherits from Person and Company classes.

Multilevel inheritance
In multilevel inheritance, a class inherits from a child class or derived class.
Suppose three classes A, B, C. A is the superclass, B is the child class of A, C is the
child class of B.

In other words, we can say a chain of classes is called multilevel


inheritance.

Example

# Base class
class Vehicle:
def Vehicle_info(self):
print('Inside Vehicle class')

# Child class
class Car(Vehicle):
def car_info(self):
print('Inside Car class')

# Child class
class SportsCar(Car):
def sports_car_info(self):
print('Inside SportsCar class')

Mr. D.S.Kiwde Page 4


Python MMN

# Create object of SportsCar


s_car = SportsCar()

# access Vehicle's and Car info using SportsCar object


s_car.Vehicle_info()
s_car.car_info()
s_car.sports_car_info()

Output

Inside Vehicle class

Inside Car class

Inside SportsCar class

 In the above example, we can see there are three classes


named Vehicle, Car, SportsCar.
 Vehicle is the superclass, Car is a child of Vehicle, SportsCar is a child of Car.
 So we can see the chaining of classes.

Hierarchical Inheritance
In Hierarchical inheritance, more than one child class is derived from a
single parent class. In other words, we can say one parent class and multiple child
classes.

Mr. D.S.Kiwde Page 5


Python MMN

Example

Let’s create ‘Vehicle’ as a parent class and two child class ‘Car’ and ‘Truck’ as a
parent class.

class Vehicle:
def info(self):
print("This is Vehicle")

class Car(Vehicle):
def car_info(self, name):
print("Car name is:", name)

class Truck(Vehicle):
def truck_info(self, name):
print("Truck name is:", name)

obj1 = Car()
obj1.info()
obj1.car_info('BMW')

obj2 = Truck()
obj2.info()
obj2.truck_info('Ford')

Output

This is Vehicle

Car name is: BMW

This is Vehicle

Truck name is: Ford

Hybrid Inheritance
When inheritance is consists of multiple types or a combination of different
inheritance is called hybrid inheritance.

Mr. D.S.Kiwde Page 6


Python MMN

Example

class Vehicle:
def vehicle_info(self):
print("Inside Vehicle class")

class Car(Vehicle):
def car_info(self):
print("Inside Car class")

class Truck(Vehicle):
def truck_info(self):
print("Inside Truck class")

# Sports Car can inherits properties of Vehicle and Car


class SportsCar(Car, Vehicle):
def sports_car_info(self):
print("Inside SportsCar class")

# create object
s_car = SportsCar()

s_car.vehicle_info()
s_car.car_info()
s_car.sports_car_info()

Note: In the above example, hierarchical and multiple inheritance exists. Here
we created, parent class Vehicle and two child classes named Car and Truck this is
hierarchical inheritance.

Another is SportsCar inherit from two parent classes named Car and Vehicle.
This is multiple inheritances.

Mr. D.S.Kiwde Page 7

You might also like