Tuesday, April 2, 2013

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:

4 comments:

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

Priya Kannan said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

calfre services said...

Thank you for posting the valuable information https://www.calfre.com/India/Hyderabad/Ameerpet/SAP-HR-ABAP-Training/listing