Close
Close

Inheritance in Perl


Inheritance is using the properties of a parent class by a child class. It means that a child class inherits the parent class. So, we are going to deal with the subclass of any class. To get a general idea, think that a square is also a rectangle, which means that it is a subclass of rectangle.
Let's make it clearer.
Suppose we define a rectangle with some length and breadth. Now, a square is also a rectangle but having the same length and breadth.

inheritance in Perl

I hope, it has given you the feel that square can be a subclass of rectangle.

Instead of writing new data members or methods, one can inherit the members of an existing class. This existing class is called the base class or superclass, and the new class is called the derived class or sub-class.

Let's make a sub-class of a super class.

#file is Student.pm

use strict;
use warnings;

package Student;

sub new{

  my $class = shift;

  my $self = {
    'name' => shift,
    'roll_number' => shift
  };

  bless $self, $class;

  return $self;
}

1;
#file is Btech.pm

package Btech;

use strict;
use warnings;

use parent 'Student';

1;
use strict;
use warnings;

use Btech;

my $a = Btech->new("Sam",01);

print "$a->{'name'}\n";
print "$a->{'roll_number'}\n";
Output
Sam
1

You can see that in the package 'Btech', we have used use parent 'Student'; to use 'Student' as a parent class. After this statement, 'Btech' becomes a subclass of 'Student' and will be able to access the properties and the methods of parent class.

Method overriding


Method overriding means having two methods with the same name but doing different tasks. It means that one of the methods overrides the other. If there is any method in a superclass and a method with the same name in subclass, then by executing a method, the method from the corresponding class will be executed.

Let's see an example of this:

#This is Rectangle.pm

use strict;
use warnings;

package Rectangle;

sub new{

  my $class = shift;

  my $self = {
    'length' =>shift,
    'breadth' =>shift
  };

  bless $self, $class;

  return $self;
}

sub getPerimeter{
  my $self = shift;
  print "Perimeter of rectangle is ",2*($self->{'length'}+$self->{'breadth'}),"\n";
}

sub getArea{
  my $self = shift;
  return $self->{'length'}*$self->{'breadth'};
}

1;
#file is Square.pm

package Square;

use strict;
use warnings;

use parent 'Rectangle';

sub getPerimeter{
  my $self = shift;
  print "Perimeter of square is ",4*($self->{'length'}),"\n";
}

1;
use strict;
use warnings;

use Square;

my $a = Rectangle->new(5,10);

my $b = Square->new(4,4);

$a->getPerimeter();
$b->getPerimeter();
Output
Perimeter of rectangle is 30
Perimeter of square is 16

As we have seen, the method from the corresponding class came into action, it means that one overrode the other.
Execution of 'getPerimeter' on the object of 'Rectangle' printed "Perimeter of rectangle is 30" from the 'getPerimeter' defined in the Rectangle class. Whereas, execution of 'getPerimeter' on the object of 'Square' printed "Perimeter of square is 16" from 'getPerimeter' defined in the Square class.

It is very useful because it prevents us from making methods with different names and remembering all of them.

SUPER


Notice that we have used my $b = Square->new(4,4); in the case of square to give the same value of length and breadth. However, it would be nice if we had to pass side only one time i.e., my $b = Square->new(4);.

This can be best achieved by using SUPER. SUPER is used to call parent class's method from subclass. It means that along with method overriding, we can also call the method from subclass. The SUPER modifier can only be used for the method calls. Let's see an example of method overriding:

#This is Rectangle.pm

use strict;
use warnings;

package Rectangle;

sub new{

  my $class = shift;

  my $self = {
    'length' =>shift,
    'breadth' =>shift
  };

  bless $self, $class;

  return $self;
}

sub getPerimeter{
  my $self = shift;
  print "Perimeter of rectangle is ",2*($self->{'length'}+$self->{'breadth'}),"\n";
}

sub getArea{
  my $self = shift;
  return $self->{'length'}*$self->{'breadth'};
}

1;
#file is Square.pm

package Square;

use strict;
use warnings;

use parent 'Rectangle';

sub new{
  my $class = shift;
  my $side = shift;
  my $self = $class->SUPER::new($side,$side);
  $self->{'name'} = "square";

  bless $self, $class;
  return $self;
}

sub getPerimeter{
  my $self = shift;
  print "Perimeter of square is ",4*($self->{'length'}),"\n";
}

1;
use strict;
use warnings;

use Square;

my $a = Rectangle->new(5,10);

my $b = Square->new(4,4);

$a->getPerimeter();
$b->getPerimeter();
print "$b->{'name'}\n";
Output
Area of rectangle is 30
Perimeter of square is 16
square

You can see that we have used my $self = $class->SUPER::new($side,$side); in Square package. We have used it to call the constructor of Rectangle class and give same values to both length and breadth (SUPER::new($side,$side)). The constructor of the Rectangle returns a reference to its object and we are making it equal to a variable '$self' of the Square class my $self = $class->SUPER::new($side,$side);.


Ask Yours
Post Yours
Doubt? Ask question