Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Friend Class

karthikeyan_m4
Explorer
0 Likes
751

I want to access private methods using friend class...

For example

CLASS C1 DEFINITION.

PUBLIC SECTION.

METHODS : M1.

PRIVATE SECTION.

METHODS: M2.

ENDCLASS.

CLASS C1 IMPLEMENTATION.

METHOD M1.

WRITE : 'Public Method C1'.

ENDMETHOD.

METHOD M2.

WRITE : 'Private Method C1'.

ENDMETHOD.

ENDCLASS.

CLASS C2 DEFINITION CREATE PRIVATE FRIENDS C1.

PUBLIC SECTION.

METHODS : M3.

ENDCLASS.

CLASS C2 IMPLEMENTATION.

METHOD M3.

data : obj TYPE REF TO C1.

create OBJECT obj.

CALL METHOD obj->M2. " Error says access to M2 is not allowed

WRITE 'Public Method C2'.

ENDMETHOD.

ENDCLASS.

Pllz help...

thanks

Karthikeyan

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
708

It is class C1 which has to declare friendship to class C2 in order C2 can access C1's private components, not vice versa.

I.e I say John is my friend (I therefore allow him to play my PS3;) ). John does not like me so much (a pitty:( ) so he doesn't allow me ride his bike.

So what you need is


CLASS c2 DEFINITION DEFERRED.

CLASS c1 DEFINITION FRIENDS c2.
  PUBLIC SECTION.
    METHODS : m1.
  PRIVATE SECTION.
    METHODS: m2.
ENDCLASS.                  


CLASS c1 IMPLEMENTATION .
  METHOD m1.
    WRITE : 'Public Method C1'.
  ENDMETHOD.                    "M1
  METHOD m2.
    WRITE : 'Private Method C1'.
  ENDMETHOD.                    "M2
ENDCLASS.                    


CLASS c2 DEFINITION FRIENDS c1.  "my friends are here, allow them access to my (C2's) private components
  PUBLIC SECTION.
    METHODS : m3.
ENDCLASS.              


CLASS c2 IMPLEMENTATION.
  METHOD m3.
    DATA : obj TYPE REF TO c1.
    CREATE OBJECT obj.
    CALL METHOD obj->m2.    "now I am able to call his method, as he says I am his friend
    WRITE 'Public Method C2'.
  ENDMETHOD.                    "M3
ENDCLASS.                   

START-OF-SELECTION.
  DATA obj_c2 TYPE REF TO c2.

  CREATE OBJECT obj_c2.
  obj_c2->m3( ).

Regards

Marcin

3 REPLIES 3
Read only

MarcinPciak
Active Contributor
0 Likes
709

It is class C1 which has to declare friendship to class C2 in order C2 can access C1's private components, not vice versa.

I.e I say John is my friend (I therefore allow him to play my PS3;) ). John does not like me so much (a pitty:( ) so he doesn't allow me ride his bike.

So what you need is


CLASS c2 DEFINITION DEFERRED.

CLASS c1 DEFINITION FRIENDS c2.
  PUBLIC SECTION.
    METHODS : m1.
  PRIVATE SECTION.
    METHODS: m2.
ENDCLASS.                  


CLASS c1 IMPLEMENTATION .
  METHOD m1.
    WRITE : 'Public Method C1'.
  ENDMETHOD.                    "M1
  METHOD m2.
    WRITE : 'Private Method C1'.
  ENDMETHOD.                    "M2
ENDCLASS.                    


CLASS c2 DEFINITION FRIENDS c1.  "my friends are here, allow them access to my (C2's) private components
  PUBLIC SECTION.
    METHODS : m3.
ENDCLASS.              


CLASS c2 IMPLEMENTATION.
  METHOD m3.
    DATA : obj TYPE REF TO c1.
    CREATE OBJECT obj.
    CALL METHOD obj->m2.    "now I am able to call his method, as he says I am his friend
    WRITE 'Public Method C2'.
  ENDMETHOD.                    "M3
ENDCLASS.                   

START-OF-SELECTION.
  DATA obj_c2 TYPE REF TO c2.

  CREATE OBJECT obj_c2.
  obj_c2->m3( ).

Regards

Marcin

Read only

0 Likes
708

This was really awesome!!!!!

Read only

hubert_heitzer
Contributor
0 Likes
708

Hi Karthikeyan,

friendship can only be declared in the class which allows friendship.

In your example class C1 has to declare class C2 as a friend.

If friendship could be declared like you tried, it would be very easy to break up an encapsulation.

Regards, Hubert