|
131 | 131 | },
|
132 | 132 | {
|
133 | 133 | "cell_type": "code",
|
134 |
| - "execution_count": null, |
| 134 | + "execution_count": 11, |
135 | 135 | "metadata": {},
|
136 |
| - "outputs": [], |
| 136 | + "outputs": [ |
| 137 | + { |
| 138 | + "name": "stdout", |
| 139 | + "output_type": "stream", |
| 140 | + "text": [ |
| 141 | + "Parrot can fly\n", |
| 142 | + "Penguin can't fly\n" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
137 | 146 | "source": [
|
138 | 147 | "### Polymorphism \n",
|
139 | 148 | "class Parrot:\n",
|
|
164 | 173 | "flying_test(blu)\n",
|
165 | 174 | "flying_test(peggy)"
|
166 | 175 | ]
|
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": 16, |
| 180 | + "metadata": {}, |
| 181 | + "outputs": [ |
| 182 | + { |
| 183 | + "name": "stdout", |
| 184 | + "output_type": "stream", |
| 185 | + "text": [ |
| 186 | + "Rahul\n" |
| 187 | + ] |
| 188 | + } |
| 189 | + ], |
| 190 | + "source": [ |
| 191 | + "# Python program to demonstrate error if we \n", |
| 192 | + "# forget to invoke __init__() of parent. \n", |
| 193 | + "\n", |
| 194 | + "class A: \n", |
| 195 | + " def __init__(self, n = 'Rahul'): \n", |
| 196 | + " self.name = n \n", |
| 197 | + "class B(A): \n", |
| 198 | + " def __init__(self, roll): \n", |
| 199 | + " A.__init__(self)\n", |
| 200 | + " self.roll = roll \n", |
| 201 | + "\n", |
| 202 | + "object = B(23) \n", |
| 203 | + "print(object.name) \n" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": 38, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [ |
| 211 | + { |
| 212 | + "name": "stdout", |
| 213 | + "output_type": "stream", |
| 214 | + "text": [ |
| 215 | + "B\n" |
| 216 | + ] |
| 217 | + } |
| 218 | + ], |
| 219 | + "source": [ |
| 220 | + "\n", |
| 221 | + "\n", |
| 222 | + "class A: \n", |
| 223 | + " def __init__(self, n = 'A'): \n", |
| 224 | + " self.name = n \n", |
| 225 | + "class B: \n", |
| 226 | + " def __init__(self, n = 'B'): \n", |
| 227 | + " self.name = n \n", |
| 228 | + "class D: \n", |
| 229 | + " def __init__(self, n = 'D'): \n", |
| 230 | + " self.name = n \n", |
| 231 | + "\n", |
| 232 | + "class C(A,B,D):\n", |
| 233 | + " # def __init__(self): \n", |
| 234 | + " # A.__init__(self)\n", |
| 235 | + " # B.__init__(self)\n", |
| 236 | + " pass\n", |
| 237 | + " \n", |
| 238 | + "\n", |
| 239 | + "object = C()\n", |
| 240 | + "print(object.name) \n" |
| 241 | + ] |
| 242 | + }, |
| 243 | + { |
| 244 | + "cell_type": "code", |
| 245 | + "execution_count": null, |
| 246 | + "metadata": {}, |
| 247 | + "outputs": [], |
| 248 | + "source": [] |
167 | 249 | }
|
168 | 250 | ],
|
169 | 251 | "metadata": {
|
|
0 commit comments