If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
mostly ज्यादातर programming languages multiple inheritance को support नहीं करती है , लेकिन Python के साथ ऐसा नहीं है। Python Multiple Inheritance को easily handle करती है।
Multiple Inheritance में किसी class द्वारा एक से ज्यादा classes को Inherit किया जाता है। मतलब कोई class A, class B, C को एक साथ Inherit कर सकती है।
Multiple inheritance को आप नीचे दिए गए graph की help से समझ सकते हैं।
Multiple Classes को Inherit करने के लिए simply parenthesis में comma से separate करके classes के name pass कर दिए जाते हैं।
# class A.
class A :
def classa_fun(self) :
print('Function in class A')
# class B.
class B :
def classb_fun(self) :
print('Function in class B')
# class C that extends class A and B.
class C(A,B) :
def classc_fun(self) :
print('Function in class C')
# now we can access all the functions using class C object that are in class A and B.
obj = C()
obj.classa_fun()
obj.classb_fun()
obj.classc_fun()
C:\Users\Rahulkumar\Desktop\python>python multiple_inhrt.py Function in class A Function in class B Function in class C
Inheritance में इस बात का ध्यान रहे कि कोई class सिर्फ एक ही बार inherit की जा सकती यही। एक class को आप बार बार inherit नहीं कर सकते हैं।
For Example -
class B(A) : pass class C (A, B) : #here we are inheriting class A again. pass It raises an error : Traceback (most recent call last): File "test.py", line 7, in <module> class C (A, B) : TypeError: Cannot create a consistent method resolution order (MRO) for bases A, B
Multiple Inheritance में parent class का कोई method या property access करने के लिए हमेशा Parent Class Name use होता है।
For Example -
# class A.
class A :
def __init__(self) :
print('Constructor in class A')
# class B.
class B :
def __init__(self) :
print('Constructor in class B')
# class C extends class A and B.
class C(A,B) :
def __init__(self) :
A.__init__(self)
B.__init__(self)
print('Constructor in class C')
# just initialize the object.
obj = C()
C:\Users\Rahulkumar\Desktop\python>python multiple_inhrt.py Constructor in class A Constructor in class B Constructor in class C
Note : Example में parent class के __init__() method को access करने के लिए Class Name use किया गया है। और अगर हो सके तो multiple inheritance में super() method के through parent class का कुछ भी access न करें इससे आपको confusion होगा। क्योंकि super() method ,पहले pass की गयी class से ही data access करता है।
For Example -
class C(A,B) :
def __init__(self) :
super().__init__()
print('Constructor in class C')
# just initialize the object.
obj = C()
Output :
Constructor in class A
Constructor in class C
example में class C(A,B) लिखा गया मतलब class A को super() method ज्यादा priority देगा। इसलिए Parent Class Name use करें जिससे कम से कम confusion रहेगा।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook