Encapsulation means one attribute and method can be modified in different classes. Hence data and method can have different form and logic and that can be hidden to separate class.
Interface is used when we need to create one method with different functionality in different classes. Here the name of the method needs not to be changed. The same method will have to be implemented in different class implementations.
The following program contains an Interface it. There we have declared attribute and a method like mit. Now we have defined two classes like cls1 and cls2. So we have to implement the method mit in both of the class implementation. We have implemented the method mit differently in different class. Now in the start of selection we create two objects obj1 and obj2 for two classes. Next we call the method by different objects and we are getting the function declared in separate class.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* INTERFACE it
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE it.
DATA v_txt TYPE char50.
METHODS mit.
ENDINTERFACE. "it
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
INTERFACES it.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
PUBLIC SECTION.
INTERFACES it.
ENDCLASS. "cls2 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD it~mit.
it~v_txt = 'Class One Interface Method'.
WRITE / it~v_txt.
ENDMETHOD. "it~mit
ENDCLASS. "cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
METHOD it~mit.
it~v_txt = 'Class Two Interface Method'.
WRITE / it~v_txt.
ENDMETHOD. "it~mit
ENDCLASS. "cls2 IMPLEMENTATION
START-OF-SELECTION.
DATA: obj1 TYPE REF TO cls1,
obj2 TYPE REF TO cls2.
CREATE OBJECT: obj1, obj2.
CALL METHOD: obj1->it~mit,
obj2->it~mit.
The output is following:
Interface is used when we need to create one method with different functionality in different classes. Here the name of the method needs not to be changed. The same method will have to be implemented in different class implementations.
The following program contains an Interface it. There we have declared attribute and a method like mit. Now we have defined two classes like cls1 and cls2. So we have to implement the method mit in both of the class implementation. We have implemented the method mit differently in different class. Now in the start of selection we create two objects obj1 and obj2 for two classes. Next we call the method by different objects and we are getting the function declared in separate class.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* INTERFACE it
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE it.
DATA v_txt TYPE char50.
METHODS mit.
ENDINTERFACE. "it
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
INTERFACES it.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
PUBLIC SECTION.
INTERFACES it.
ENDCLASS. "cls2 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD it~mit.
it~v_txt = 'Class One Interface Method'.
WRITE / it~v_txt.
ENDMETHOD. "it~mit
ENDCLASS. "cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
METHOD it~mit.
it~v_txt = 'Class Two Interface Method'.
WRITE / it~v_txt.
ENDMETHOD. "it~mit
ENDCLASS. "cls2 IMPLEMENTATION
START-OF-SELECTION.
DATA: obj1 TYPE REF TO cls1,
obj2 TYPE REF TO cls2.
CREATE OBJECT: obj1, obj2.
CALL METHOD: obj1->it~mit,
obj2->it~mit.
The output is following:
1 comment:
Thanks for the great information in your blog SAP Success FactorsTraining in Chennai
Post a Comment