Broad Network


Inheritance in Perl OOP

Perl Object Oriented Programming – Part 3

Perl Course

Foreword: In this part of the series, I talk about the ability to define new classes using an existing class as a basis; that is, inheritance.

By: Chrysanthus Date Published: 26 Sep 2015

Introduction

This is part 3 of my series, Perl Object Oriented Programming. In this part of the series, I talk about the ability to define new classes using an existing class as a basis; that is, inheritance. You should have read the previous parts of the series before coming here, as this is a continuation.

New Attributes and New Methods
Attributes and methods of a class can together be called, members of the class. The word, “members” is a term I have borrowed from C++. You can have a class with its attributes and methods; then you want a new class that will have those same attributes and methods, plus new members. Are you going to describe (create) 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 plus new members. So, if you want a new class that has extra members in addition to what an existing class has, you inherit it from the existing class (see below), adding the new members.

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

use strict;

    {package Calculator;

        sub new
            {
                bless {}, $_[0];
            }

        sub add
            {
                my $ref = $_[0];
                my $sum = $ref->{'num1'} + $ref->{'num2'};
                return $sum;
            }

    }

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

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

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 6) 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 attribute, which will hold the fixed value (6). It will have an additional method that will square the sum and add the fixed value. It inherits the two attributes and the add() method of the existing class. The syntax of the statement to derive one class from another is:

    @SubClassName::ISA = ("ParentClassName");

or

    use parent "parentClassName";

Note: This statement is placed inside the derived class description at it’s beginning. For the first statement, the left operand (of the assignment operator) including the ISA word, is an array. SubClassName is the derived (inherited) class name of your choice. The right operand has the array list, which consists of one element (in this case), which is the parent class name, in quotes. The second statement is used, when the parent class is in a different file.

The following script shows a Calculator class and a derived class for the Calculator. The derived class is called, ChildCalculator and it has one attribute and one method. The attribute has the fixed value. The method takes as argument, the result of the addition from the Calculator class, and squares it. The parent class is of course, Calculator.

use strict;

    {package Calculator;

        sub new
            {
                bless {num1=>2, num2=>3}, $_[0];
            }

        sub add
            {
                my $ref = $_[0];
                my $sum = $ref->{num1} + $ref->{num2};
                return $sum;
            }

    }

    {package ChildCalculator;

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

        sub squareAdd
            {
                my $ref = $_[0];
                my $finalVal = $_[1] * $_[1] + $ref->{'fixedValue'};
                return $finalVal;
            }

    }

    my $myChildObj = ChildCalculator->new();
    $myChildObj->{'fixedValue'} = 6;
    my $result = $myChildObj->add();
    my $endResult = $myChildObj->squareAdd($result);
    print $endResult;

In the code, the Calculator parent class is a normal class with an anonymous blessed hash. Now, in Perl, if you want any attributes of the parent class to be inherited, you have to do hard code initialization to the parent’s new method, as shown above. The inherited object will then have a copy of such attributes. The parent object and the inherited object are two different objects.

After the Calculator class description above, 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 subclass (derived class) uses the same constructor as the parent class, but produces an object (hash) different from what the parent class would produce.

The code segment below the derived class description, instantiates a derived class object. It then allocates the attribute/value pair for the fixed value of 6. It goes on to use the inherited add() method in the derived object and then it uses the derived object’s squareAdd() method.

A child object can access the attributes of the parent object if such attributes were hard coded in the constructor of the parent. The child object ends up with a copy of such attributes. A child object can access the methods of the parent class.

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

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message