Summary -
In this topic, we described about the below sections -
Encapsulation is an object-oriented programming concept that binds the code/functions and data together to manipulate the data. Encapsulation keeps code/functions and data both safe from outside interference and misuse.
Encapsulation is a protective cover that prevents the code and data being randomly accessed by other code that defines outside the wrapper. Access to the code and data inside wrapper is tightly controlled through well-defined interface.
ABAP supports the encapsulation properties and data hiding through the creating classes. There are three levels of visibility in Object-Oriented ABAP and those are -
- Public
- Protected
- Private
By default, all items defined in a class are private.
Encapsulation by Interface -
Encapsulation means one attribute and one method can be modified in different classes. Hence data and method can have different form, logic and can be hidden to separate class.
Interface is used to create one method with different functionality in different classes. Here the name of the method needs not be changed. The same method can have implemented in different class implementations.
Example -
Simple example to implement encapsulation in application program.
Code -
*&---------------------------------------------------------------------*
*& Report Z_ENCAPSULATION
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_ENCAPSULATION.
* Interface Definition
INTERFACE interfacenew.
* Interface method in interface
METHODS methodnew.
ENDINTERFACE.
* classnew1 Definition
CLASS classnew1 DEFINITION.
PUBLIC SECTION.
* Interface method in classnew1 definition
INTERFACES interfacenew.
ENDCLASS.
* classnew1 implementation
CLASS classnew1 IMPLEMENTATION.
* Interface method implementation in calssnew1 implementation
METHOD interfacenew~methodnew.
WRITE:/ 'Interface methodnew from classnew1 executing..'.
ENDMETHOD.
ENDCLASS.
* classnew2 Definition
CLASS classnew2 DEFINITION.
PUBLIC SECTION.
* Interface method in classnew2 definition
INTERFACES interfacenew.
ENDCLASS.
* classnew2 implementation
CLASS classnew2 IMPLEMENTATION.
* Interface method implementation in calssnew1 implementation
METHOD interfacenew~methodnew.
WRITE:/ 'Interface methodnew from classnew2 executing..'.
ENDMETHOD.
ENDCLASS.
* Creatng class object
START-OF-SELECTION.
* Declaring and creating object for classnew1
DATA: childobj1 TYPE REF TO classnew1.
CREATE OBJECT childobj1.
WRITE /'Accessing methodnew through childclass1 object...'.
* Calling interface method using childobj1 object
CALL METHOD: childobj1->interfacenew~methodnew.
ULINE.
* Declaring and creating object for classnew2
DATA: childobj2 TYPE REF TO classnew2.
CREATE OBJECT childobj2.
WRITE /'Accessing methodnew through childclass2 object...'.
* Calling interface method using childobj2 object
CALL METHOD: childobj2->interfacenew~methodnew.
ULINE.
Output -
Explaining Example -
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
interfacenew is the interface defined with method methodnew. Classnew1 and classnew2 are the two classes defined with interface interfacenew. So these two classes can access interfacenew components.
classnew1 and classnew2 implemented the interface method methodnew differently in each class. So the methodnew has a different form/logic in both classes which is a Encapsulation.