-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inhritance.py
167 lines (133 loc) · 3.36 KB
/
Inhritance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'''
Inheritance : it is the mechanism of creating new class from existing one,
so that the newly created class can inherit and use the menbers of existing class.
old class from which we create the new one is called as ---> Base class /Parent class/Super Class
and newly created class is called as ---> Derived class /child class / sub class
syntax:
class Parent:
parent data and methods
class Child(Parent):
data and methods of child
Python supports 5 types of inheritance:
Single
Multilevel
Multiple
Hierachycal
Hybrid
# Single inheritance
class A:
def __init__(self):
self.varA = 10
def m1(self):
print('This is method of class A')
class B(A):
def __init__(self):
self.varB = 20
def m2(self):
print('this is class B derived from class A')
obj = B()
print(obj.varB)
print(obj.varA)
obj.m1()
obj.m2()
#########################################################################################
# Miltilevel Inheritance
class A:
def m1(self):
print('This is method 1 form class A')
class B(A):
def m2(self):
print('This is method 2 from class B')
class C(B):
def m3(self):
print('This is method 3 from class C')
class D(C):
def m4(self):
self.m1()
self.m2()
self.m3()
print('This is method 4 from class D')
objD = D()
objD.m4()
#########################################################################################
# Multiple inheritance
class A:
def m1(self):
print('From Class A')
class B:
def m2(self):
print('From B')
class C:
def m3(self):
print('From C')
class D(A,B,C): # class D is having three parents namely class A, Class B, class C
def m4(self):
print('From class D')
self.m1()
self.m2()
self.m3()
objD = D()
objD.m4()
objD.m1()
####################################################################################################################
# Hierachycal inheritance
class A:
def m1(self):
self.a = 10
print('this is method from class A')
class B(A):
def m2(self):
print("This is method of class B")
class C(A):
def m3(self):
print('This is method from class C')
# we have created the child classes from A---> class B and class C
objB = B()
objC = C()
objB.m2()
objB.m1()
#print(objC.a)
#print(objB.a)
objC.m1()
objC.m3()
################################################################################################################################
#Hybrid inheritance
class A:
def m1(self):
self.a = 10
print('this is method from class A')
class B:
def m2(self):
print("This is method of class B")
class C(A,B):
def m3(self):
print('This is method from class C')
# we have created the child classes from A---> class B and class C
class E(C):
pass
class F(C):
def m6(self):
print('This is class F created from class C')
objB = B()
objC = C()
objB.m2()
#print(objC.a)
#print(objB.a)
objC.m1()
objC.m3()
objF= F()
objF.m1()
'''
class A:
def __init__(self):
self.name = 'Bunty'
def m1(self):
self.college = 'ACS'
class B():
def __init__(self, roll):
A.m1(self)
A.__init__(self)
self.roll = roll
object = B(23)
print(object.name)
print(object.college)