Social Media

7/16/2019

Multiple Inheritance

Multiple Inheritance denotes that a class has more than one parent classes.
In Java language multiple inheritance is not possible.
We can take advantage of multiple inheritance in Python language.
The Syntax for multiple inheritance can be as:
class A:
   ....
   ....
class B:
   ...
   ...
class C(A,B):
   ...
   ...
Script: Write an script in Python to demonstrate the use of Multiple Inheritance.
class A:
   def __init__(self,a):
      self.a=a
   def showA(self):
      print("the value of a=",self.a)

class B:

   def __init__(self,b):
      self.b=b
   def showB(self):
      print("the value of b=",self.b)

class C(A,B):

   def __init__(self,a,b,c):
      A.__init__(self,a)
      B.__init__(self,b)
      self.c=c
   def showC(self):
      A.showA(self)
      B.showB(self)
      print("the value of c=",self.c)

obj=C(2,3,4)

obj.showC()
print(obj.__dict__)


Output:
the value of a= 2
the value of b= 3
the value of c= 4
{'a': 2, 'b': 3, 'c': 4}



Be First to Post Comment !
Post a Comment