Broad Network


Overriding and Modifying Subclass Methods in Perl OOP

Perl Object Oriented Programming Part 7

Perl Course

Foreword: In this part of the series, I explain how to override a method in an inherited Class and how to modify a method in an inherited class.

By: Chrysanthus Date Published: 26 Sep 2015

Introduction

This is part 7 of my series, Perl Object Oriented Programming. In this part of the series, I explain how to override a method in an inherited Class and how to modify a method in an inherited class. In the parent class, you can have a method, and in the derived class you may want a method with the same name but that does a slightly different operation. In this case, you override the method in the subclass; that is, you give the method in the subclass, a different content, but with same method name.

If you do not want to override the method, then you can modify a subclass method with the SUPER modifier (always in uppercase). In this case, you call the parent method within the definition of the child method, using the SUPER modifier. The rest of the code within the child method is new. The child method can have a different name compared to that of the parent method.

You must have read the previous part of the series before coming here, as this is a continuation.

Overriding a Public Method
The following code illustrates how to override a public method. The name of the public method is meth(). Read and try it.

use strict;

    {package TheCla;

        my %ha = (wordA => "pencil", wordB => "ruler");
        $ha{coderef} = sub
                    {
                        print "I have a $ha{wordA} and a $ha{wordB}", "\n";
                    };

        sub meth
            {
                print "Do you have a $ha{wordA}, my child?", "\n";
            }

        sub new
            {
                bless {word1 =>"pen", word2=>"book"}, $_[0];
            }
    }

    {package TheSub;
        
        @TheSub::ISA = ("TheCla");

        sub meth
            {
                print "No, but I have a color pencil.", "\n";
            }

    }

    my $obj = TheCla->new();
    $obj->meth();

    my $childObj = TheSub->new();
    $childObj->meth();

The output is:

    Do you have a pencil, my child?
    No, but I have a color pencil.

There are two classes here: the parent class and the subclass. The parent class has a private hash with two private attributes and one private method. It also has two public attributes and one public method. The subclass overrides the public method. The method in the subclass is public. The overriding method in the subclass has the same name as the name in the parent. However, the body of the overriding method is different. You call the overriding method with the subclass name or the subclass object reference. You call the parent public method with the parent class name or the parent object reference.

Overriding a Private Method
The following code illustrates how to override a private method. The variable of the private method in the parent is $ha{coderef}. Read and try the code.

use strict;

    {package TheCla;

        my %ha = (wordA => "pencil", wordB => "ruler");
        $ha{coderef} = sub
                    {
                        print "I have a $ha{wordA} and a $ha{wordB}", "\n";
                    };

        sub meth
            {
                print "Do you have a $ha{wordA}, my child?", "\n";
            }

        sub new
            {
                bless {word1 =>"pen", word2=>"book"}, $_[0];
            }
    }

    {package TheSub;
        
        @TheSub::ISA = ("TheCla");

        my %ha;
        $ha{coderef} = sub
                    {
                        print "I know your secret, Daddy.", "\n";
                    };

        sub getChild
            {
                return undef unless ((ref($_[0]) eq "TheSub")||($_[0] eq "TheSub"));
                return $ha{$_[1]};
            }
    }

    my $childObj = TheSub->new();
    &{$childObj->getChild("coderef")}();

The output is:

    I know your secret, Daddy.

To override a private method following the Perl OOP scheme I am presenting to you, you just need to have a private hash (my) in the subclass. The hash has a code reference to the overridden function. The code reference name is the same as the one in the parent. The body of the method is different. Such an overridden method is independent of the parent method. It has to be accessed by a method in the subclass, such as the getChild() method. The getChild() method, will only allow access to its class private member, if the call is coming from its object or its class, otherwise, it returns undef.

Modifying a Public Method
You can modify a subclass method by calling a parent method within the definition of the subclass method. You use the SUPER modifier for this. The syntax to use the SUPER modifier is:

    $object->SUPER::method()

The following code shows how a public method can be modified in a subclass. Read and try the code:

use strict;

    {package TheCla;

        my %ha = (wordA => "pencil", wordB => "ruler");
        $ha{coderef} = sub
                    {
                        print "I have a $ha{wordA} and a $ha{wordB}", "\n";
                    };

        sub meth
            {
                print "Do you have a $ha{wordA}, my child?", "\n";
            }

        sub new
            {
                bless {word1 =>"pen", word2=>"book"}, $_[0];
            }
    }

    {package TheSub;
        
        @TheSub::ISA = ("TheCla");

        sub childMeth
            {
                print "Daddy, please repeat the question.", "\n";
                $_[0]->SUPER::meth();
                print "No, but I have a color pencil.", "\n";
            }

    }

    my $childObj = TheSub->new();
    $childObj->childMeth();

The output is:

Daddy, please repeat the question.
Do you have a pencil, my child?
No, but I have a color pencil.

Modifying a Private Method
With the Perl OOP scheme I have been presenting to you, it is not convenient to use the SUPER modifier to call the parent private method within a subclass method. Instead, use the protected getParent() method approach. The following code shows how a private method can be modified in a subclass. Read and try the code:

use strict;

    {package TheCla;

        my %ha = (wordA => "pencil", wordB => "ruler");
        $ha{coderef} = sub
                    {
                        print "I have a $ha{wordA} and a $ha{wordB}", "\n";
                    };

        sub getParent
            {
                return undef unless ((ref($_[0]) eq "TheSub")||($_[0] eq "TheSub"));
                return $ha{$_[1]};
            }

        sub meth
            {
                print "Do you have a $ha{wordA}, my child?", "\n";
            }

        sub new
            {
                bless {word1 =>"pen", word2=>"book"}, $_[0];
            }
    }

    {package TheSub;
        
        @TheSub::ISA = ("TheCla");

        my %ha;
        $ha{cr} = sub
                    {
                        print "Daddy, please repeat what you said.", "\n";
                        &{TheSub->getParent("coderef")}();
                        print "I know your secret, Daddy.", "\n";
                    };

        sub getChild
            {
                return undef unless ((ref($_[0]) eq "TheSub")||($_[0] eq "TheSub"));
                return $ha{$_[1]};
            }
    }

    my $childObj = TheSub->new();
    &{$childObj->getChild("cr")}();

Well, let us end here for this part of the series. We 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