Type of Inheritance
Reuse with inheritance
Objective
Type of Inheritance
Single level Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single level Inheritance
Single Ineritance
class Vehicle:
def __init__(self, name, mileage, capacity):
self.name = name
self.mileage = mileage
self.capacity = capacity
def fare(self):
return self.capacity * 100
class Bus(Vehicle):
pass
School_bus = Bus("School Volvo", 12, 50)
print("Total Bus fare is:", School_bus.fare())
Single level Inheritance
class Job:
def __init__(self, person_name):
self.name = person_name
def task(self):
print("working")
Output:
class Teacher(Job): teach students
def task(self):
print("teach students")
teacher = Teacher("xiaoxu")
teacher.task()
Single level Inheritance
class Job:
def __init__(self, person_name):
self.name = person_name
def task(self):
print("working")
class Teacher(Job):
def __init__(self, person_name, school): Output:
super().__init__(person_name) AMRITA VISHWA VIDYAPEETHAM
self.school = school VISHAL
Teaching
def task(self):
print("Teaching")
teach1 = Teacher("VISHAL", "AMRITA
VISHWA VIDYAPEETHAM")
print(teach1.school)
print(teach1.name)
teach1.task()
Single level Inheritance
Output:
Total Bus fare is: 5000
Multilevel Inheritance
It is a process where an inherited class is further
made as a parent class to another class
Multilevel Inheritance
Multilevel Inheritance
Output:
I am a Student who belongs to Person class
Engineering Student inheriting properties
from Student
Multilevel Inheritance
Output:
Student inheriting properties from Person
I am a Student who belongs to Person class
Engineering Student inheriting properties from
Student
ARJUN CB.EN.U4EIE18134 EIE
Multiple Inheritance
The process where a single class acquiring the behaviors and
capabilities from multiple classes called multiple inheritance
Multiple Inheritance
Multiple Inheritance
Output:
Father : Mark
Mother : Sonia
Multiple Inheritance
Output:
John
102
Multiple Inheritance
Output:
Richard
Hierarchical inheritance
he process where multiple classes inherit the capabilities from a single base clas
called Hierarchical inheritance
Hierarchical inheritance
Output:
I am a Person
I am a Student who is Person
I am a Teacher who is a Person
I am a Doctor who is a Person
Hybrid inheritance
The inheritance in which the derivation of a class involves more than
one form of any inheritance is called hybrid inheritance. Basically
hybrid inheritance is combination of two or more types of
inheritance. It can also be called multi path inheritance.
combination of single inheritance and multiple
inheritance.
Example:1
Example:2
Example:3
combination of hierarchical and
multiple inheritance
Important Points in Inheritance