Friendship is one sided

Friendship is one sided


Friendship is one sided. That means if a class cls2 is defined with Friends of cls1 then cls1 can access all of protected and private components of cls2 but not vice versa. If cls2 want to access all components of cls1 then cls1 will have to be defined by Friends of cls2. Otherwise compile error will come in case of accessing cls1 components like below:


We have Modified this program by defining the cls1 class as Friends of cls2. Not the output is coming properly.


*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

CLASS cls1 DEFINITION DEFERRED.

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION FRIENDS cls1.
  PUBLIC SECTION.
    METHODS meth2.
  PROTECTED SECTION.
    DATA v2_pro TYPE char50.
  PRIVATE SECTION.
    DATA v2_pri TYPE char50.
    METHODS m_cls2.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION FRIENDS cls2.
  PUBLIC SECTION.
    METHODS meth1.
  PROTECTED SECTION.
    DATA v1_pro TYPE char50.
  PRIVATE SECTION.
    DATA v1_pri TYPE char50.
    METHODS m_cls1.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD meth2.
    DATA obj1 TYPE REF TO cls1.
    CREATE OBJECT obj1.
    CALL METHOD obj1->m_cls1.
  ENDMETHOD.                                                "meth2

  METHOD m_cls2.
    v2_pro = 'Protected Data Class 2'.
    v2_pri = 'Private Data Class 2'.
    WRITE: / 'Private Method of Class 2:',
           / v2_pro,
           / v2_pri.
    SKIP.
  ENDMETHOD.                                                "m_cls2
ENDCLASS.                    "cls2 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD meth1.
    DATA obj2 TYPE REF TO cls2.
    CREATE OBJECT obj2.
    CALL METHOD obj2->m_cls2.
  ENDMETHOD.                                                "meth1

  METHOD m_cls1.
    v1_pro = 'Protected Data Class 1'.
    v1_pri = 'Private Data Class 1'.
    WRITE: / 'Private Method of Class 1:',
           / v1_pro,
           / v1_pri.
    SKIP.
  ENDMETHOD.                                                "m_cls1
ENDCLASS.                    "cls1 IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj1 TYPE REF TO cls1,
        obj2 TYPE REF TO cls2.

  CREATE OBJECT: obj1, obj2.
  CALL METHOD: obj2->meth2,
               obj1->meth1.


Below is the output:

Share this

Related Posts