Posted By Naveen Singh on 24 May 2023
python-program
Python में inheritance basically पांच प्रकार के होते है।
• single inheritance
• multiple inheritance
• multilevel inheritance
• hierarchical inheritance
• hybrid inheritance
जब कोई single child class केवल single parent class को inherit करता है, तो इसे single inheritance कहते है।
Example-:
# Python program to demonstrate
# single inheritance
# Base class
class parent(object):
def parent_method(self):
print("This is a parent class")
# Derived class
class child(parent):
def child_method(self):
print("This is a child class")
# Driver's code
obj = child()
obj.parent_method()
obj.child_method()
Output-:
This is a parent class
This is a child class
जब कोई child class एक से ज़्यादा parent class(base class) को inherit करे तो इस प्रकार के inheritance को हम multiple inheritance कहते है। multiple inheritance में, base class के
सभी features को parent class(derived class) में inherit किया जा सकता हैं।
Example-:
# Python program to demonstrate
# multiple inheritance
# Base class 1
class class1():
def first(self):
print("First class")
# Base class 2
class class2():
def second(self):
print("Second class")
# Derived class
class class3(class1,class2):
def third(self):
print("Third class")
# Driver's code
obj = class3()
obj.first()
obj.second()
obj.third()
Output-:
First class
Second class
Third class
Multilevel inheritance में, parent class को child A inherit करता है जिसका मतलब है की parent class की properties child A में inherit हो जाती हैं। जिसके बाद क्या होता है child B,
child A को inherit कर लेता है जिससे parent class की सारी properties child B में inherit हो जाती है। और इसी को हम multilevel inheritance कहते हैं। यह एक child और grandfather के
relation को represent करता है।
Example-:
# Python program to demonstrate
# multilevel inheritance
# Base class
class classA:
def _init_(self, student1):
self.student1 = student1
# Intermediate class
class classB(classA):
def _init_(self, student2, student1):
self.student2 = student2
# invoking constructor of Grandfather class
classA._init_(self,student1)
# Derived class
class classC(classB):
def _init_(self, student3, student2, student1):
self.student3 = student3
# invoking constructor of Father class
classB._init_(self, student2, student1)
def display_name(self):
print('First student name :', self.student1)
print("Second student name :", self.student2)
print("Third student name :", self.student3)
# Driver code
obj = classC("Rohit", "Ayush", "Manish")
print(obj.student1)
obj.display_name()
Output-:
Manish
First student name : Manish
Second student name : Ayush
Third student name : Rohit
जब एक ही parent class की सहायता से एक से अधिक child class बनाए जाते हैं तो इस प्रकार के inheritance को हम hierarchical inheritance कहते है। इस program में, हमारे पास एक
parent (base) class और दो child (derived) class होते हैं।
Example-:
# Hierarchical inheritance
# Base class
class classA:
def parent(self):
print("Parent class")
# Derived class1
class classB(classA):
def childA(self):
print("First child class")
# Derivied class2
class classC(classA):
def childB(self):
print("Second child class")
# Driver's code
obj1= classB()
obj2= classC()
obj1.parent()
obj1.childA()
obj2.parent()
obj2.childB()
Output-:
Parent class
First child class
Parent class
Second child class
वह inheritance जो कई सारे inheritance से मिलकर बना होता उसे हम hybrid inheritance कहते है।