-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism.py
More file actions
40 lines (25 loc) · 1003 Bytes
/
polymorphism.py
File metadata and controls
40 lines (25 loc) · 1003 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
# Polymorphism is a greek word that means to have many forms
# Two ways to achieve polymorphism: Inheritance and Duck Typing
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape): # a circle object is of type Circle and type Shape i.e. has many forms
def __init__(self, radius):
self.radius = radius
def area(self):
return f"{3.14 * self.radius ** 2} cmsq"
class Triangle(Shape): # a triangle object is of type Triangle and type Shape i.e. has many forms
def __init__(self, side):
self.side = side
def area(self):
return f"{0.5 * self.side ** 2} cmsq"
class Square(Shape): # a square object is of type Square and type Shape i.e. has many forms
def __init__(self, side):
self.side = side
def area(self):
return f"{self.side ** 2.0} cmsq"
shapes = [Circle(3), Triangle(4), Square(5)]
for shape in shapes:
print(shape.area())