Polymorphism means having vivid or different forms. In the programming world, Polymorphism refers to the ability of the function with the same name to carry different functionality altogether. It creates a structure that can use many forms of objects. This permits
functions/arguments to use entities of different types at different times. In object-oriented programming, Polymorphism allows a particular object referring to a particular class to be used in a similar fashion as if it was a different object referring to altogether a different class. Implementing Polymorphism in Python with ClassPython can use different types of classes, in the same way, using Polymorphism. To serve this purpose, one can create a loop that iterates through a tuple of objects. Post which, one can call the methods without having a look at the type of class to which the object belongs to. Example: Polymorphism with Classes and Objectsclass Rabbit(): def age(self): print("This function determines the age of Rabbit.") def color(self): print("This function determines the color of Rabbit.") class Horse(): def age(self): print("This function determines the age of Horse.") def color(self): print("This function determines the color of Horse.") obj1 = Rabbit() obj2 = Horse() for type in (obj1, obj2): # creating a loop to iterate through the obj1, obj2 type.age() type.color() Output: This function determines the age of Rabbit. Implementing Polymorphism in Python with InheritanceWe will be defining functions in the derived class that has the same name as the functions in the base class. Here, we re-implement the functions in the derived class. The phenomenon of re-implementing a function in the derived class is known as Method Overriding. Example: Polymorphism with Inheritanceclass Animal: def type(self): print("Various types of animals") def age(self): print("Age of the animal.") class Rabbit(Animal): def age(self): print("Age of rabbit.") class Horse(Animal): def age(self): print("Age of horse.") obj_animal = Animal() obj_rabbit = Rabbit() obj_horse = Horse() obj_animal.type() obj_animal.age() obj_rabbit.type() obj_rabbit.age() obj_horse.type() obj_horse.age() Output: Various types of animals Recommended Readings:
Compile-Time Polymorphism or Method Overloading?Unlike many other popular object-oriented programming languages such as Java, Python doesn’t support compile-time polymorphism or method overloading. If a class or Python script has multiple methods with the same name, the method defined in the last will override the earlier one. Python doesn’t use function arguments for method signature, that’s why method overloading is not supported in Python. Operator Overloading in PythonPython supports operator overloading. This is another type of polymorphism where an operator behaves differently based on the type of the operands.
Read More at Operator Overloading in Python. Advantages of Polymorphism
References
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: Examples of user-defined polymorphic functions: Output: Polymorphism with Class MethodsBelow 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 ObjectsWe 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. |