When you lookup for the meaning of word “Polymorphism” , you get “many forms” or “ability to take multiple forms”. What does this mean in terms of programming?
#Example 1 var1 = 2 var2 = 3 var1 + var2 >>5 #Example 2 str1 = "Hello !!!" str2 = " How have you been?" str1 + str2 >> Hello !!! How have you been
Here the addition operator is adding two numbers in the first example and in the second it is concatenating strings this is polymorphism.
Following is an example demonstrating Polymorphism, assume there classes for shapes , and each shape has a draw method, this method draws the shape. To improvise the code lets make the Shape class as Abstract and then let other class implement the methods within the shape class.
""" Inheritance Implementation """ import abc class Shape(object): """ Abstract Class""" __metaclass__ = abc.ABCMeta @abc.abstractmethod def draw(self): return class Textbox(Shape): def draw(self): print("Draw Textbox") class Checkbox(Shape): def draw(self): print("Drawing Checkbox") if __name__ == "__main__": obj = Checkbox() obj.draw() obj_2 = Textbox() obj_2.draw()
With polymorphism in place we can reduce so much redundant code.
Hope this was helpful