Skip to content
Related Articles

Related Articles

Improve Article

Perl | Inheritance in OOPs

  • Last Updated : 04 Jul, 2019
Geek Week

Inheritance is the ability of any class to extract and use features of other classes. It is the process by which new classes called the derived classes are created from existing classes called Base classes. The basic concept here is that the programmer is able to use the features of one class into another without declaring or defining the same thing again and again in different classes. Instead of writing the member functions during every class declaration we can inherit those from the base class. Inheritance is one of the most important feature of Object Oriented Programming.

Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
Super Class: The class whose properties are inherited by sub class is called Base Class or Super class

The most basic concept of inheritance is Creating or Deriving a new class using another class as a base.

Base Class and Derived class

Base class is used to derive additional inherited subclasses known as Derived classes. There can be multiple derived classes from a single base class, such type of inheritance is called Hierarchical Inheritance. These derived classes share a single Parent class or Base class. If a derived class shares multiple Parent classes and inherit its features from multiple parent classes then such kind of inheritance is called Multiple Inheritance.


Above image shows the order in which a class is derived from a base class. Whenever there is need to depict the order of inheritance theoretically, the order and the denotations as shown in the above image will be used.



Consider a class of Vehicles. Now we need to create a class for Bus, Car, truck, etc. The methods fuelAmount(), capacity(), applyBrakes() will be the same for all of the Vehicles. If we create these classes without the knowledge of inheritance then we might do it the way as shown in the diagram:

Above Image shows the creation of these classes without the concept of Inheritance

It can be seen clearly that the above process results in duplication of the same code 3 times. This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used. If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability. Look at the below diagram in which the three classes are inherited from vehicle class:


Using inheritance, we have to write the functions only one time instead of three times as we have inherited the rest of the three classes from the base class(Vehicle).

Multilevel Inheritance

Inheritance in Perl can be of many types but multilevel inheritance is one in which there is a chain of the base class and derived classes. In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C

Implementing Inheritance in Perl:

Inheritance in Perl can be implemented with the use of packages. Packages are used to create a parent class which can be used in the derived classes to inherit the functionalities.




use strict;
use warnings;
  
# Creating parent class
package Employee;
  
# Creating constructor
sub new
{
    # shift will take package name 'employee' 
    # and assign it to variable 'class'
    my $class = shift;
      
    my $self = {
                'name' => shift,
                'employee_id' => shift
               };
      
    # Bless function to bind object to class
    bless $self, $class;
      
    # returning object from constructor
    return $self;
}
1;

The above code is the definition of the base class. Here the base class is employee with the data members being the employee id and the name of the employee. This code for parent class needs to be saved as *.pm, here we will save it as employee.pm. We’ll now see how to derive a class from the already declared base class employee.




# Creating parent class
package Department;
   
use strict;
use warnings;
   
# Using class employee as parent 
use parent 'employee';
   
1;

As seen in the above example, the class Department uses the traits of the already declared class employee. So while declaring the class Department we did not declare all the data members again instead inherited them from the base class employee. To run this code, save the intermediatory class code as *.pm, here it is saved as Department.pm. This class is the intermediatory class and will further work as a parent class for the following given derived file data.pl.




use strict;
use warnings;
   
# Using Department class as parent
use Department;
  
# Creating object and assigning values 
my $a = Department->new("Shikhar",18017);
   
# Printing the required fields
print "$a->{'name'}\n";
print "$a->{'employee_id'}\n";

Output:

Thus inheritance plays a very vital role when working on a big project and the programmer wants to shorten the code.




My Personal Notes arrow_drop_up
Recommended Articles
Page :