Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
pramodrepaka
Participant

Everyone knows what exactly a friendship is and how its is used.

#used ???

I mean in OOABAP how it is used  ..... We are in SCN so most of us know what exactly I am talking about ; this blog is exclusively for the people who are new to this topic. Lets not waste much time just scan through. A sample code is also written, keeping in mind that this topic would be much crisp and clear.

Following the SAP K.I.S.S way.(Keep It Short and Simple)


1.Friendship between classes:-

A Class can grant friendship to another class. By granting friendship, it allows another class to:-

  • Use its private components.
  • Instatiate it, irrespective of the CREATE PRIVATE addition.

Ex:-

1. Class C2 is created using CREATE PRIVATE option. That means only the class itself and its friends can instantiate this class.

2. Class C2 has a private method M2 and a private attribute, NUM. This means that these components can be accessed by class C2          itself and its friends.

3. Now C2 has granted friendship to class C1.

4. So, methods of class C1 can access private components of C2 as well as can instantiate class C2.

Program:-

CLASS C1 DEFINITION DEFERRED.

CLASS C2 DEFINITION CREATE PRIVATE FRIENDS C1.

    PROTECTED SECTION.

       DATA: NUM TYPE I VALUE 5.

       METHODS : M2.

ENDCLASS.

CLASS C2 IMPLEMENTATION.

   METHOD M2.

      WRITE:/5 'I am method M2 in C2'.

    ENDMETHOD.

ENDCLASS.

CLASS C1 DEFINITION.

    PUBLIC SECTION.

     METHODS: M1.

ENDCLASS.

CLASS C1 IMPLEMENTATION.

   METHOD M1.

    DATA: OREF2 TYPE REF TO C2.

    CREATE OBJECT OREF2.

    WRITE:/5 OREF2->NUM.

    CALL METHOD OREF2->M2.

    ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

   DATA: OREF1 TYPE REF TO C1.

   CREATE OBJECT OREF1.

   CALL METHOD OREF1->M1.

Output:-

5

I am method M2 in C2.

2.Subclasses of friends can also become friends:-

Subclasses of the friend class are also friends of the class granting friendship (to their super classes).

Ex:-

1. Class C2 has granted friendship to class C1. Hence, C1 is friend of class C2.

2. Class C11 is a sub class of class C1.

3. So, class C11 is also a friend of class C2. Class C11 can thus access the protected components of class C2.

Program:-

CLASS C1 DEFINITION DEFERRED.

CLASS C2 DEFINITION FRIENDS C1.

      PROTECTED SECTION.

       DATA: NUM TYPE I VALUE 5.

ENDCLASS.

CLASS C2 IMPLEMENTATION.

ENDCLASS.

CLASS C1 DEFINITION.

    PUBLIC SECTION.

      METHODS: M1.

ENDCLASS.

CLASS C1 IMPLEMENTATION.

      METHOD M1.

           DATA: OREF2 TYPE REF TO C2.

           CREATE OBJECT OREF2.

           WRITE:/5 OREF2->NUM.

       ENDMETHOD.

ENDCLASS.

CLASS C11 DEFINITION INHERITING FROM C1.

    PUBLIC SECTION.

      METHODS: M11.

ENDCLASS.

CLASS C11 IMPLEMENTATION.

      METHOD M11.

         DATA: OREF2 TYPE REF TO C2.

         CREATE OBJECT OREF2.

         WRITE:/5 OREF2->NUM.

       ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA: OREF11 TYPE REF TO C11.

CREATE OBJECT OREF11.

CALL METHOD OREF11->M11.

Output:-

5

3.Friendship is one sided:-

In principle, granting of friendship is one-sided: A class granting a friendship is not automatically a friend of its friends. If the class granting the friendship wants to access the private components of a friend, then the latter has to explicitly grant friendship to the former.


Ex:-

1. Class C2 grants friendship to class C1. Hence, class C1 can access protected attribute (num2) of C2.

2. But, Class C2 cannot access protected attribute (num1) of class C1. This is because friendship is one-sided.

3. To allow C2 access protected attribute of C1, class C1 must also declare C2 as its friend.

Program:-

Let us do the incorrect way first

Wrong way:-

CLASS C1 DEFINITION DEFERRED.

CLASS C2 DEFINITION FRIENDS C1.
PROTECTED SECTION.
DATA:NUM2 TYPE I VALUE 15.
METHODS: M2.
ENDCLASS.

CLASS C1 DEFINITION.
PUBLIC SECTION.
METHODS:METHPUB.
PRIVATE SECTION.
DATA: NUM1 TYPE I VALUE 10.
METHODS: M1.
ENDCLASS.

CLASS C2 IMPLEMENTATION.
METHOD  M2.
DATA: OREF1 TYPE REF TO C1.
CREATE OBJECT OREF1.
WRITE:/5 OREF1->NUM1.
ENDMETHOD.
ENDCLASS.

CLASS C1 IMPLEMENTATION.
METHOD M1.
DATA: OREF2 TYPE REF TO C2.
CREATE OBJECT OREF2.
WRITE:/5 OREF2->NUM2.
ENDMETHOD.

METHOD METHPUB.
CALL METHOD M1.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: OREF TYPE REF TO C1.
CREATE OBJECT OREF.
CALL METHOD OREF->METHPUB.

Output:- ERROR: Access to private attribute “NUM1” is not allowed.

This is what happens if you dont offer friendship

Correct way:-

CLASS C1 DEFINITION DEFERRED.

CLASS C2 DEFINITION FRIENDS C1.
PROTECTED SECTION.
DATA:NUM2 TYPE I VALUE 15.
METHODS: M2.
ENDCLASS.

CLASS C1 DEFINITION FRIENDS C2.
PUBLIC SECTION.
METHODS:METHPUB.
PRIVATE SECTION.
DATA: NUM1 TYPE I VALUE 10.
METHODS: M1.
ENDCLASS.

CLASS C2 IMPLEMENTATION.
METHOD  M2.
DATA: OREF1 TYPE REF TO C1.
CREATE OBJECT OREF1.
WRITE:/5 OREF1->NUM1.
ENDMETHOD.
ENDCLASS.

CLASS C1 IMPLEMENTATION.
METHOD M1.
DATA: OREF2 TYPE REF TO C2.
CREATE OBJECT OREF2.
WRITE:/5 OREF2->NUM2.
ENDMETHOD.

METHOD METHPUB.
CALL METHOD M1.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: OREF TYPE REF TO C1.
CREATE OBJECT OREF.
CALL METHOD OREF->METHPUB.

Output:-

15

5 Comments