Polymorphism in Python


 Polymorphism

  Polymorphism contains two words "poly" and "morph", poly means many and morphs means forms, shape. By polymorphism, we understand that one task can be performed in different ways. for example, you have a class animal, and all animals speak. But they speak differently. Here the speak behavior is polymorphic in the sense and depends on the animal.

in python polymorphism enables using a single interface with input of different data types, different class or may be for different numbers of input.

polymorphism is the ability to do one task in many styles.

Example

len("Hello")                               # Return 5 as result

len([1,2,3,4,45,345,23,42])        # Return 8 as result

In this case the function len() is polymorphic as it is taking string as input in the first case and is taking the list as input in the second case.

In Python, Polymorphism is a way of making a function accept objects of different classes if they behave similarly.

There are two ways to achieve polymorphism, in Python  programming.

1. Method Overloading

2. Method Overriding


No comments:

Post a Comment