|
1 | 1 | ## Multiple Inheritance |
2 | 2 |
|
3 | | -### Introduction |
4 | | -- When a child class inherits from multiple parent classes, it is called as multiple inheritance. |
5 | | -- We specify all parent classes as comma separated list in bracket. |
| 3 | +- When a single **child class** inherit's from multiple parent classes,it is called as **Multiple inheritance.** |
| 4 | +- We specify all parent Class as comma seprated list in child class. |
| 5 | +- if two parent class have same method and you calling that method using object of child class then the method call is dependent on which class is intialized first. |
| 6 | + |
| 7 | +Example: |
| 8 | +```python |
| 9 | +def __init__(self): |
| 10 | + A.__init(self) |
| 11 | + B.__init(self) |
| 12 | +``` |
| 13 | +here class A method is called First if that method not present in class A then it will execute it from class B |
| 14 | +```python |
| 15 | +def __init__(self): |
| 16 | + B.__init(self) |
| 17 | + A.__init(self) |
| 18 | +``` |
| 19 | +Here class B method is called First |
6 | 20 |
|
7 | 21 |  |
8 | 22 |
|
9 | 23 | ### Algorithm |
10 | | - |
11 | | -### Syntax |
12 | | - |
| 24 | +1. create a parent class |
| 25 | +```python |
| 26 | +class A: |
| 27 | +``` |
| 28 | +2. Define methods and attributes |
| 29 | +```python |
| 30 | +def __init__(self): |
| 31 | + self.varsa="I am from class A" |
| 32 | +``` |
| 33 | +3. Create a Another Parent class named B |
| 34 | +```python |
| 35 | +class B: |
| 36 | +``` |
| 37 | +4. Define methods and attributes for class B |
| 38 | +```python |
| 39 | +def __init__(self): |
| 40 | + self.varsb="I am from class B |
| 41 | + ``` |
| 42 | +5. Now create a child class by inheriting the class A and Class B |
| 43 | +```python |
| 44 | +class C(A,B): |
| 45 | +``` |
| 46 | +6. Initilize and call the constructor of class A and Class B in Construction oof class C |
| 47 | +```python |
| 48 | +def __init__(self): |
| 49 | + A.__init__(self) |
| 50 | + B.__init__(self) |
| 51 | +``` |
| 52 | +7. Now you can use class A and Class B variables in Class C |
| 53 | +```python |
| 54 | +def __init__(self): |
| 55 | + A.__init__(self) |
| 56 | + B.__init__(self) |
| 57 | + #Get data of two parent classes using self |
| 58 | + print(self.varsa) |
| 59 | + print(self.varsb) |
| 60 | +``` |
| 61 | +8.Then create a object of child class and call required methods and atributes using it |
| 62 | +```python |
| 63 | +c1=C() |
| 64 | +``` |
13 | 65 | ### Example 1 |
14 | 66 | ```python |
15 | 67 | class A: |
16 | | - def __init__(self): |
17 | | - print("Class A Is Initilized") |
18 | | - self.Avar="I am A Class variable" |
| 68 | + def __init__(self): |
| 69 | + self.name='Python' |
19 | 70 | class B(): |
20 | | - def __init__(self): |
21 | | - print("Class B Is Initilized") |
22 | | - self.Bvar="I am B Class variable" |
23 | | - |
| 71 | + def __init__(self): |
| 72 | + self.age=20 |
24 | 73 | class C(B,A): |
25 | 74 | def __init__(self): |
26 | | - print("Class C Is Initilized") |
27 | | - B.__init__(self) |
| 75 | + B.__init__(self) #First B class is Initilized |
28 | 76 | A.__init__(self) |
29 | | - |
30 | | - print(self.Bvar) |
31 | | - print(self.Avar) |
32 | | -b1=C() |
| 77 | + self.year=2019 |
| 78 | + def ShowData(self): |
| 79 | + print(self.name) |
| 80 | + print(self.age) |
| 81 | + print(self.year) |
| 82 | +c1=C() |
| 83 | +c1.ShowData() |
33 | 84 | ''' |
34 | | -#Result: |
35 | | -Class C Is Initilized |
36 | | -Class B Is Initilized |
37 | | -Class A Is Initilized |
38 | | -I am B Class variable |
39 | | -I am A Class variable |
| 85 | +#Result |
| 86 | +Python |
| 87 | +20 |
| 88 | +2019 |
40 | 89 | ''' |
41 | 90 | ``` |
42 | 91 |
|
43 | 92 | ### Example 2 |
| 93 | +```python |
| 94 | +#Parent class 1 |
| 95 | +class A: |
| 96 | + def __init__(self): |
| 97 | + print("class A is Initilized") |
| 98 | + self.name='Python' |
| 99 | + |
| 100 | +#parent class 2 |
| 101 | +class B(): |
| 102 | + def __init__(self): |
| 103 | + print("Class B is Initilized") |
| 104 | + self.age=20 |
| 105 | + |
| 106 | +#child class C inherit from parent class 1 and parent class 2 |
| 107 | +class C(B,A): |
| 108 | + def __init__(self): |
| 109 | + print("Class C is Initilized") |
| 110 | + |
| 111 | + #initilize constructor B |
| 112 | + B.__init__(self) |
| 113 | + |
| 114 | + #initilize constructor A |
| 115 | + A.__init__(self) |
| 116 | + |
| 117 | + #Initilize C instance Variable |
| 118 | + self.year=2019 |
| 119 | + |
| 120 | + def ShowData(self): |
| 121 | + print(self.name) |
| 122 | + print(self.age) |
| 123 | + print(self.year) |
| 124 | + |
| 125 | +#Create object of C class |
| 126 | +c1=C() |
| 127 | + |
| 128 | +#Call method of C Class |
| 129 | +c1.ShowData() |
| 130 | + |
| 131 | +''' |
| 132 | +#Result |
| 133 | +Class C is Initilized |
| 134 | +Class B is Initilized |
| 135 | +class A is Initilized |
| 136 | +Python |
| 137 | +20 |
| 138 | +2019 |
| 139 | +''' |
| 140 | +``` |
0 commit comments