Skip to content
Related Articles

Related Articles

Perl | Polymorphism in OOPs
  • Difficulty Level : Medium
  • Last Updated : 07 Jul, 2019

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:




use warnings;
  
# Creating class using package
package A;
  
# Constructor creation
sub new
{
   
    # shift will take package name 'vehicle' 
    # and assign it to variable 'class'
    my $class = shift;
    my $self = {
                  'name' => shift,
                  'roll_no' => shift
               };
      
    sub poly_example
    {
      print("This corresponds to class A\n");
    }
};
  
package B;
  
# The @ISA array contains a list 
# of that class's parent classes, if any
my @ISA = (A);
  
sub poly_example
{
  print("This corresponds to class B\n");
  
}
  
package main;
  
B->poly_example();
A->poly_example();

Output:

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :