In this Python Polymorphism tutorial, you will learn:
What is Polymorphism? Polymorphism in Operators Polymorphism in user-defined methods Polymorphism in Functions Polymorphism and Inheritance Polymorphism with the Class Methods Difference between Method overloading and compile-time Polymorphism
Polymorphism in Operators
An operator in Python helps perform mathematical and several other programming tasks. For example, the ‘+’ operator helps in performing addition between two integer types in Python, and in the same way, the same operator helps in concatenating strings in Python programming. Let us take an example of + (plus) operator in Python to display an application of Polymorphism in Python as shown below: Python Code: Output: The above example can also be regarded as the example of operator overloading.
Polymorphism in user-defined methods
A user-defined method in the Python programming language are methods that the user creates, and it is declared using the keyword def with the function name. Polymorphism in the Python programming language is achieved through method overloading and overriding. Python defines methods with def keyword and with the same name in both child and parent class. Let us take the following example as shown below: – Python Code: Output: In the above code, there are two user-defined methods, perimeter and area, defined in circle and square classes. As shown above, both circle class and square class invoke the same method name displaying the characteristic of Polymorphism to deliver the required output.
Polymorphism in Functions
The built-in functions in Python are designed and made compatible to execute several data types. In Python, Len() is one of the key built-in functions. It works on several data types: list, tuple, string, and dictionary. The Len () function returns definite information aligned with these many data types. The following figure shows how Polymorphism can be applied in Python with relation to in-built functions: –
Following program helps in illustrating the application of Polymorphism in Python: – Python Code: Output:
In the above example, Len () function of Python performs Polymorphism for string, list, and dictionary data types, respectively.
Polymorphism and Inheritance
Inheritance in Python can be defined as the programming concept wherein a child class defined inherit properties from another base class present in Python. There are two key Python concepts termed method overriding and method overloading.
In method overloading, Python provides the feature of creating methods that have the same name to perform or execute different functionalities in a given piece of code. It allows to overload methods and uses them to perform different tasks in simpler terms. In Method overriding, Python overrides the value that shares a similar name in parent and child classes.
Let us take the following example of Polymorphism and inheritance as shown below: – Python Code: Output: In above code, the methods have the same name defined as init method and area1 method. The object of class square and rectangle are then used to invoke the two methods to perform different tasks and provide the output of the area of square and rectangle.
Polymorphism with the Class Methods
The Python programming enables programmers to achieve Polymorphism and method overloading with class methods. The different classes in Python can have methods that are declared in the same name across the Python code. In Python, two different classes can be defined. One would be child class, and it derives attributes from another defined class termed as parent class. The following example illustrates the concept of Polymorphism with class methods: – Python Code: Output: In the above code, two different classes named as flipkart and amazon use the same method names info and init to provide respective price quotations of the product and further illustrate the concept of Polymorphism in Python.
Difference between Method overloading and compile-time Polymorphism
In compile-time Polymorphism, the compiler of the Python program resolves the call. Compile-time Polymorphism is accomplished through method overloading. The Python compiler does not resolve the calls during run time for polymorphism. It is also classified as method overriding wherein the same methods carry similar signatures or properties, but they form a part of different classes.
Summary:
Polymorphism can be defined as a condition that occurs in many different forms. An operator in Python helps perform mathematical and several other programming tasks. A user-defined method in the Python programming language are methods that the user creates, and it is declared using the keyword def with the function name. Polymorphism in Python offers several desirable qualities, such as it promotes the reusability of codes written for different classes and methods. A child class is a derived class, and it gets its attributes from the parent class. The Polymorphism is also achieved through run-time method overriding and compile-time method overloading. Polymorphism in Python is also attained through operator overloading and class methods.