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).
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.
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.
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.
Thus inheritance plays a very vital role when working on a big project and the programmer wants to shorten the code.
- Perl | Encapsulation in OOPs
- Perl | Methods in OOPs
- Perl | Polymorphism in OOPs
- Perl | Objects in OOPs
- Perl | Method Overriding in OOPs
- Object Oriented Programming (OOPs) in Perl
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- OOPs | Object Oriented Design
- OOPS | Generalization as extension and restriction using Java
- Multiple Inheritance in PHP
- Perl vs C/C++
- Perl | References
- Perl | tr Operator
- Perl | Operators | Set - 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.