Visitar URL original
Update multipleinheritance.md · VenkatAdd/PythonScript@f42e2a9 · GitHub
Skip to content

Commit f42e2a9

Browse files
authored
Update multipleinheritance.md
1 parent 5172099 commit f42e2a9

1 file changed

Lines changed: 122 additions & 25 deletions

File tree

‎Class/multipleinheritance.md‎

Lines changed: 122 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,140 @@
11
## Multiple Inheritance
22

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
620

721
![Multiple Inheritance](https://github.com/chavarera/PythonScript/blob/master/Class/multipleinheritance.png)
822

923
### 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+
```
1365
### Example 1
1466
```python
1567
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'
1970
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
2473
class C(B,A):
2574
def __init__(self):
26-
print("Class C Is Initilized")
27-
B.__init__(self)
75+
B.__init__(self) #First B class is Initilized
2876
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()
3384
'''
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
4089
'''
4190
```
4291

4392
### 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

Comments
 (0)