Broad Network


Abstract Class in Perl OOP

Perl Object Oriented Programming Part 11

Perl Course

Foreword: In this part of the series I talk about what is known as abstract class and abstract method in Perl.

By: Chrysanthus Date Published: 26 Sep 2015

Introduction

This is part 11 of my series, Perl Object Oriented Programming. In this part of the series I talk about what is known as abstract class and abstract method in Perl. You should have read the previous parts of the series before reaching here; this is a continuation.

Abstract Method
An abstract method is a method in the parent class that does not have the method body. The method body is given to the overriding method in the derived class. The method should be used only in the derived class; it should not be used in the parent class.

Abstract Class
An abstract class is one having an abstract method. An abstract class is normally not useful. However I give the reason for the coding of abstract classes below. It is the derived classes that are usually used. Read and try the following program:

use strict;

    {package Calculator;

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

        sub add;
    }

    {package ChildCalculator;

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

        sub add
            {
                my $sum = $_[1] + $_[2];
                return $sum;
            }
    }

    my $childObj = ChildCalculator->new();
    my $result = $childObj->add(2,3);
    print $result;

In the program, the add() method is the abstract method and the parent class is the abstract class. In the parent class description above, the add() method declaration, does not have the parentheses.

When the add() function is called by the instantiated object, $_[0] is the reference to the object (not used above),  $_[1] is the first argument (e.g. 2 above) and $_[2] is the second argument (e.g. 3 above).

Reason for Abstract Class
You may write a class today; you know the method, but you have not yet decided on the method implementation. In that case you create the abstract class. In future you implement the method. In some cases, the method might even be implemented by a different person. It is also possible that with time, the implementation code may be changed; in that case, the (super) class structure is left intact and not affected.

That is it for this part of the series.

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

Comments

Become the Writer's Fan
Send the Writer a Message