Polymorphism in Python:
|We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.|
(1) Polymorphism is an important feature of the class definition in Python that is utilized when you have commonly named methods across classes or subclasses. This allows functions to use objects of any of these polymorphic classes without needing to be aware of distinctions across the classes. Polymorphism can be carried out through inheritance, with subclasses making use of base class methods or overriding them.
In the above example, we have used the same len function containing different data types i.e. list and string. That’s why it is called Polymorphism.
Important Concepts of Polymorphism in Python:
(2) Method or Function Overloading- In Python, you can create a method/function that can be called in different ways. So, you can have a method that has zero, one, or more number of parameters. Depending on the method definition, we can call it zero, one, or more arguments. Given a single method or function, the member of parameters can be specified by you. This process of calling the same method in different ways is called method function overloading.
(3) Method or Function Overriding- It is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. When a method in a subclass has the same name, the same parameters or signature, and the same return type (or sub-type) as a method in its superclass, then the method in the subclass is said to override the method in the superclass.