Skip to content

Commit 80c6ce7

Browse files
committed
add polymorphism python
1 parent c4e932b commit 80c6ce7

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

polymorphism python.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Polymorphism in Python
2+
3+
# Polymorphism with class methods:
4+
class India():
5+
def capital(self):
6+
print("New Delhi is the capital of India.")
7+
8+
def language(self):
9+
print("Hindi the primary language of India.")
10+
11+
def type(self):
12+
print("India is a developing country.")
13+
14+
class USA():
15+
def capital(self):
16+
print("Washington, D.C. is the capital of USA.")
17+
18+
def language(self):
19+
print("English is the primary language of USA.")
20+
21+
def type(self):
22+
print("USA is a developed country.")
23+
24+
obj_ind = India()
25+
obj_usa = USA()
26+
for country in (obj_ind, obj_usa):
27+
country.capital()
28+
country.language()
29+
country.type()
30+
#########################
31+
print("")
32+
print("#####################################")
33+
print("")
34+
#########################
35+
36+
## Polymorphism with Inheritance:
37+
38+
class Bird:
39+
def intro(self):
40+
print("There are many types of birds.")
41+
42+
def flight(self):
43+
print("Most of the birds can fly but some cannot.")
44+
45+
class sparrow(Bird):
46+
def flight(self):
47+
print("Sparrows can fly.")
48+
49+
class ostrich(Bird):
50+
def flight(self):
51+
print("Ostriches cannot fly.")
52+
53+
obj_bird = Bird()
54+
obj_spr = sparrow()
55+
obj_ost = ostrich()
56+
57+
obj_bird.intro()
58+
obj_bird.flight()
59+
60+
obj_spr.intro()
61+
obj_spr.flight()
62+
63+
obj_ost.intro()
64+
obj_ost.flight()

0 commit comments

Comments
 (0)