Perl | Encapsulation in OOPs

Encapsulation in Perl is the process of wrapping up of data to protect it from the outside sources which need not have access to that part of the code. Encapsulation is a part of the Object-oriented programming, it is used to bind the data and the subroutines that are used to manipulate that data.
In a different way, encapsulation is a protective shield that prevents the data from being accessed by the code outside this shield.

  • Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
  • As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
  • Encapsulation can be achieved by: Declaring all the variables in the class as local and fetching methods of the class by importing packages to set and get the values of variables.

Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc. The finance section handles all the financial transactions and keeps records of all the data related to finance. Similarly, the sales section handles all the sales related activities and keep records of all the sales. Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name “sales section”.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Declaration and definition of Base class 
use strict; 
use warnings; 
   
package Student;
sub new 
    
    # shift will take package name 'Student'  
    # and assign it to variable 'class' 
    my $class = shift
        
    my $self = { 
                'name'=> shift
                'age'=> shift,
                'roll_no' => shift
               }; 
        
    # Bless function to bind object to class 
    bless $self, $class
        
    # returning object from constructor 
    return $self
    
# Method for displaying the details
sub get_details 
    my $self = shift
       
    print "Name is: $self->{'name'}\n"
    print "Age is: $self->{'age'}\n"
    print "Roll_no is: $self->{'roll_no'}"
     
# Object creation and calling
my $obj1 = Student->new("Rahul", 25, 12);
$obj1->get_details();

chevron_right


Output:

Name is: Rahul
Age is: 25
Roll_no is: 12

In the above code, if there is a need to access the data of the class for any modifications or just to print the data of the class then it can not be done directly. There is a need to create an object of that class and then access the data using the get_details() method. This process is termed as Data Encapsulation.

Advantages of Encapsulation:

  • Data Hiding: The user will have no idea about the inner implementation of the class. It will not be visible to the user that how the class is stored values in the variables. He only knows that we are passing the values to accessors and variables are getting initialized to that value.
  • Increased Flexibility: We can make the variables of the class as read-only or write-only depending on our requirement. If we wish to make the variables as read-only then we have to only use Get Accessor in the code. If we wish to make the variables as write-only then we have to only use Set Accessor.
  • Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.
  • Testing code is easy: Encapsulated code is easy to test for unit testing.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.