-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphism.py
41 lines (32 loc) · 1.06 KB
/
polymorphism.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
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 13:44:24 2020
@author: saura
"""
class User():
def signed_in(self):
print("User is logged in.")
def attack(self):
print("Do nothing.")
class Wizard(User):
def __init__(self, name, power):
self.name = name
self.power = power
def attack(self):
User.attack(self) # Just a way to use parent class method.
print(f"{self.name} is attacking with {self.power} power.")
class Archer(User):
def __init__(self, name, arrow):
self.name = name
self.arrow = arrow
def attack(self):
print(f"{self.name} is attacking with {self.arrow} arrows.")
wizard1 = Wizard("John", 50)
archer1 = Archer("Mohan", 85)
wizard1.attack()
archer1.attack()
# notice that these two instances are executing the child class method and not the parent class method.
'''
This is polymorphism.
Even though we are using the same function, it is giving different output based on the object.
'''