Broad Network


Inheritance in Perl Classes

Object Oriented Programming in Perl - Part 3

Forward: Inheritance is the ability to define new classes using existing classes as a basis. I explain that in this part of the series.

By: Chrysanthus Date Published: 2 Mar 2013

Introduction

This is part 3 of my series, Object Oriented Programming in Perl. You should have read the previous part of the series before reaching here. Inheritance is the ability to define new classes using existing classes as a basis. I explain that in this part of the series.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

New Properties and New Methods
Properties and methods of a class can together be called, members of the class. You can have a class with its properties and methods; then you want a new class that will have those same properties and methods, plus new members. Are you going to describe (created) a new class retyping the same old members of the existing class plus new members? Perl exists in such a way that you can have a class with its members and then a new related class with the same members and new members. So, if you want a new class that has extra members in addition to what an existing class has, you inherit (see below) it from the existing class, adding the new members.

Example
The following program shows a class with two properties and one method. The method adds the values of the two properties:

use strict;

print "Content-Type: text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Code Sample</title>\n";
print "</head>\n";
print "<body>\n";


    {package Calculator;

        my $self = {};

        sub new
            {
                my $class = $_[0];
                bless $self, $class;
            }

        sub add
            {
                my $sum = $self->{'num1'} + $self->{'num2'};
                return $sum;
            }

    }


    my $obj = Calculator->new();
    $obj->{'num1'} = 2;
    $obj->{'num2'} = 3;

    my $result = $obj->add();
    print $result;


print "</body>\n";
print "</html>\n";


Imagine that you want a class that would square a sum (a sum is the addition of two values) and add a fixed value (say 5) to the square. We already have a class that does addition of two values. So, we can derive a class from this existing class. The derived class is the inherited class. It will have an additional property, which will hold the fixed value (5). It will have an additional method that will square the sum and add the fixed value. It inherits the two properties and the add() method of the existing class. The syntax to derive a class from another is:

    @DerivedClassName::ISA = ("BaseClassName");

Note: This statement is placed inside the derived class description at it’s beginning. The left operand is an array. ISA is a reserved word for this inheritance array. DerivedClassName is the derived (inherited) class name of your choice. The right operand has the array list, which consists of one element, which is the base class name, in quotes.

The following code shows a Calculator class and a derived class for the calculator. The derived class is called, ChildCalculator and it has one property and one method. The property has the fixed value. The method takes as argument, the result of the addition from the inherited Calculator class, and squares it. The base class is of course, Calculator.

use strict;

print "Content-Type: text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Code Sample</title>\n";
print "</head>\n";
print "<body>\n";


    {package Calculator;

        my $self = {};

        sub new
            {
                my $class = $_[0];
                bless $self, $class;
            }

        sub add
            {
                my $sum = $self->{'num1'} + $self->{'num2'};
                return $sum;
            }

    }

    {package ChildCalculator;

        @ChildCalculator::ISA = ("Calculator");

        my %childHash =(fixedValue => 5);

        sub squareAdd
            {
                my $finalVal = $_[1] * $_[1] + $childHash{'fixedValue'};
                return $finalVal;
            }

    }

    my $myChildObj = ChildCalculator->new();
    $myChildObj->{'num1'} = 2;
    $myChildObj->{'num2'} = 3;
    my $result = $myChildObj->add();
    my $endResult = $myChildObj->squareAdd($result);
    print $endResult;



print "</body>\n";
print "</html>\n";

In the code, the Calculator base class is just a normal class. After that you have the description for the derived class, ChildCalculator. The first statement in the derived class description is what produces the inheritance; note this statement. The second statement is a hash with the fixed value. The properties of a class in perl are normally elements of the hash. The hash of the derived class does not have to be the one used to create the reference of the derived class object. It also does not have to return a reference to a variable.

In the code, you have the method that is for the derived class alone. Note how it is defined. After the derived class above, you have the code segment that instantiates the inherited class, gives the values for addition to the inherited class object, calls the inherited add() method and then calls the new method of the inherited class. Read and try the above code.

What is inherited from Base Class?
The members (properties and methods) of the base class are inherited. The constructor method is also inherited.

That is it for this part of the series; we stop here and continue in the next part.

Chrys

Related Links

Perl Reference
Object Oriented Programming in Perl
Date and Time in Perl
Regular Expressions in Perl
Perl Course
Web Development Course
Major in Website Design

BACK

Comments

Become the Writer's Fan
Send the Writer a Message