The word Polymorphism is made by two Greek words, Poly meaning many and Morph meaning forms. In programming, it refers to the same object exhibiting different forms and behaviors.
For example, there is a parent class: Shape, and there are four child classes extended from the parent class: Rectangle, Circle, Polygon, and Diamond. Suppose you need methods to calculate the area of each specific shape. You could define separate methods in each class, getSquareArea(), getCircleArea(). But this makes it difficult to remember each method’s name. Is it possible to just define one method called getArea()?
The base class declares a function getArea() without providing an implementation. Each derived class inherits the function declaration and provides its own implementation.
# Parent Class
class Shape:
def __init__(self):
self.sides = 0 def getArea(self):
passclass Rectangle(Shape):
def __init__(self, width=0, height=0):
self.width = width
self.height = height
self.sides = 1 def getArea(self):
return (self.width * self.height)class Circle(Shape):
def __init__(self, radius=0):
self.radius = radius def getArea(self):
return (self.radius * self.radius * 3.14)
These derived classes inherit the getArea() method, and provide a shape-specific…