Thursday, January 17, 2013
11:00 PM

Perl Script: Creating Method Override (Polymorphism)

Polymorphism means that methods defined in the base class will override methods defined in the parent classes and is mainly used to add or extend the functionality of an existing class without reprogramming the whole class.

The following simple Perl code demonstrate the concepts of Polymorphism (Method overriding):

Source: Polymorphism.pl
#!/usr/bin/perl

package parent;
  sub foo {
    print "Inside the parent. \n";
  }

# Inheritance is accomplished by placing the names of parent classes into a special array called @ISA.

package child;
  @ISA = (A);
  sub foo {
    print "Inside the child. \n";
  }
 
package main;
  child->foo();
 
Output: perl Polymorphism.pl
Inside the child.



0 comments:

Post a Comment