Polymorphism in Python

What is polymorphism? Polymorphism refers to having multiple forms. Polymorphism is a programming term that refers to the use of the same function name, but with different signatures, for multiple types.

Example of in-built polymorphic functions:

Output:

10
4

Examples of user-defined polymorphic functions:

Output:

29
597

Polymorphism with Class Methods

Below is an example of how Python can use different types of classes in the same way. For loops that iterate through multiple objects are created. Next, call the methods without caring about what class each object belongs to. These methods are assumed to exist in every class.

Example:

Output:

Javatpoint is a website out of many availabe on net.
Python is out of many topics about technology on Javatpoint.
Javatpoint is an developed website.
Pinkvilla is a website out of many availabe on net.
Celebrities is out of many topics.
pinkvilla is a developing website.

Polymorphism with Inheritance:

Polymorphism allows us to define methods in Python that are the same as methods in the parent classes. In inheritance, the methods of the parent class are passed to the child class. It is possible to change a method that a child class has inherited from its parent class. This is especially useful when the method that was inherited from the parent doesn't fit the child's class. We re-implement such methods in the child classes. This is Method Overriding.

Example:

Output:

There are multiple types of birds in the world.
Many of these birds can fly but some cannot.
There are multiple types of birds in the world.
Sparrows are the bird which can fly.
There are multiple types of birds in the world.
Ostriches are the birds which cannot fly.

Polymorphism with a Function and Objects

We can also create functions that can take any object. This allows for polymorphism. Let's take the following example: let's make a function called "func()", which will take an object we will call "obj". Even though we use the name "obj", any object that is instantiated will be able to call into this function. Let's next to give the function something it can do with the 'obj object passed to it. Let's call these three methods websites(), topic(), and type(). Each of them is defined in the classes' xyz' and 'PQR'. If we don't already have instantiations of the 'xyz" and 'PQR classes, let us create them. We can then call their actions using the same function func().

Example:

Output:

Javatpoint is a website out of many availabe on net.
Python is out of many topics about technology on Javatpoint.
Javatpoint is a developed website.
Pinkvilla is a website out of many availabe on net. .
Celebrities is out of many topics.
pinkvilla is a developing website.

Code: Implementing Polymorphism with a Function

Output:

Javatpoint is a website out of many availabe on net.
Python is out of many topics about technology on Javatpoint.
Javatpoint is a developed website.
Pinkvilla is a website out of many availabe on net. .
Celebrities is out of many topics.
pinkvilla is a developing website.





Latest Courses