-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism.py
More file actions
44 lines (34 loc) · 948 Bytes
/
polymorphism.py
File metadata and controls
44 lines (34 loc) · 948 Bytes
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
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
# Polymorphic function
def animal_sound(animal):
print(animal.speak())
animal_sound(Dog()) # Output: Woof!
animal_sound(Cat()) # Output: Cat!
# ------------------------------------------ #
class Myclass1:
def show(self):
print("This is Myclass1")
class Myclass2(Myclass1):
def show(self):
print("This is Myclass2")
obj=Myclass2()
obj.show()
class Myclass3:
def show(self,name=None,age=None):
if name is not None and age is not None:
print("Hello",name,age)
elif name is not None:
print("Hello" ,name)
elif age is not None:
print("Hello",age)
else:
print("Hello World")
obj=Myclass3()
# obj.show()
# obj.show("Jaffer 23")
obj.show(name="Jaffer",age=24)