SAP ABAP : OOPS Concept - Encapsulation

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, it 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 its own class in which it is declared.
  • As in encapsulation, the data in a class is hidden from other classes using the data hiding concept which is achieved by making the members or methods of a class private, and the class is exposed to the end-user or the world without providing any details behind implementation using the abstraction concept, so it is also known as a combination of data-hiding and abstraction.
  • Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables

Example :


REPORT ztest_demo_sup.
DATA name TYPE char10.
class lcl_animal DEFINITION"class declaration
  PUBLIC SECTION"access modifier
   METHODS m_setName IMPORTING im_name TYPE char10"behaviours instance method
   METHODS m_getname EXPORTING em_name TYPE char10.
  PRIVATE SECTION.
   DATA name TYPE char10."attributes
   DATA color TYPE char10.

ENDCLASS.

DATA(lv_objNEW lcl_animal).
lv_obj->m_setnameEXPORTING im_name 'Dog').
lv_obj->m_getnameIMPORTING em_name name ).

WRITE / name.
CLASS lcl_animal IMPLEMENTATION.
  METHOD m_setname.
    name im_name.
  ENDMETHOD.
  METHOD m_getname.
    em_name name.
  ENDMETHOD.
ENDCLASS.

                                        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 how the class is storing values in the variables. The user will only know that we are passing the values to a setter method (m_setname in above example) and variables are getting initialized with that value.
  • Increased Flexibility: We can make the variables of the class as read-only or write-know that only depending on our requirement. If we wish to make the variables read-only then we have to omit the setter methods like m_setName() . from the above program or if we wish to make the variables as write-only then we have to omit the get methods like m_getName() from the above program
  • 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.


Comments

Popular posts from this blog

SAP ABAP : OOPS Concept - Classes And Objects

SAP ABAP : OOPS Concept - Inheritance