What is meant by polymorphism in python?Give suitable example.
What is meant by polymorphism in python?
980
28-Mar-2023
Aryan Kumar
20-Apr-2023Polymorphism is the ability of objects to take on multiple forms or perform different operations based on the context in which they are used. In Python, polymorphism is achieved through the use of method overriding and method overloading.
Method overriding is a mechanism in which a subclass provides a different implementation of a method that is already defined in its superclass. This allows the subclass to inherit the method from its superclass but also gives it the ability to customize the method's behavior.
Method overloading is a mechanism in which a class can define multiple methods with the same name but with different parameters. When the method is called, the appropriate version of the method is selected based on the number and types of arguments passed to it.
Here is an example of polymorphism in Python using method overriding:
Krishnapriya Rajeev
01-Apr-2023Polymorphism is an extremely important concept in programming, wherein a single entity is used to represent different types in different situations. In Python, these entities can be operators, methods, or classes.
1. Polymorphism of '+' operator:
The '+' operator is used to perform arithmetic addition operations for integer data types.
For string data types, the same '+' operator is used to perform concatenation.
2. Polymorphism of functions:
Some functions in Python are compatible with multiple data types. An example of such a function is the len() function.
3. Polymorphism of classes:
We can use polymorphism when creating class methods since Python allows different classes to have methods with the same name. We can call these methods without specifying the object as shown: