Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as
poly means many and
morphism means types. Polymorphism is one of the most important concept of object oriented programming language. The most common use of polymorphism in object-oriented programming occurs when a parent class reference is used to refer to a child class object. Here we will see how to represent any function in many types and many forms.
Real life example of polymorphism, a person at the same time can have different roles to play in life. Like a woman at the same time is a mother, a wife, an employee and a daughter. So the same person has to have many features but has to implement each as per the situation and the condition. Polymorphism is considered as one of the important features of Object Oriented Programming.
Polymorphism is the key power of object-oriented programming. It is so important that languages that don’t support polymorphism cannot advertise themselves as Object-Oriented languages. Languages that possess classes but have no ability of polymorphism are called object-based languages. Thus it is very vital for an object-oriented programming language.
It is the ability of an object or reference to take many forms in different instances. It implements the concept of function overloading, function overriding and virtual functions.
Polymorphism is a property through which any message can be sent to objects of multiple classes, and every object has the tendency to respond in an appropriate way depending on the class properties.
This means that polymorphism is the method in an object-oriented programming language that does different things depending on the class of the object which calls it. For example, $square->area() will return the area of a square, but $triangle->area() might return the area of a triangle. On the other hand, $object->area() would have to calculate the area according to which class $object was called.
Polymorphism can be best explained with the help of the following example:
For the first output, the method poly_example() defined in class B overrides the definition that was inherited from class A and vice-versa for the second output. This enables to add or extend the functionality of any pre-existing package without re-writing the entire definition of the whole class again and again. Thus making it easy for the programmer.
- Perl | Objects in OOPs
- Perl | Inheritance in OOPs
- Perl | Encapsulation in OOPs
- Perl | Methods in OOPs
- Perl | Method Overriding in OOPs
- Object Oriented Programming (OOPs) in Perl
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Scala | Polymorphism
- Polymorphism in Python
- Polymorphism in Java
- Polymorphism in Ruby
- OOPs | Object Oriented Design
- OOPS | Generalization as extension and restriction using Java
- Perl vs C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.