Search code examples
classoopabapencapsulation

Access static methods and attributes of the friend class in ABAP


This is how my two global classes look like:

CLASS zcl_singleton_class DEFINITION CREATE PRIVATE friends ZCL_FLINFO 
PUBLIC SECTION. 

CLASS-METHODS: 
CLASS_CONSTRUCTOR,
get_instance 
    RETURNING VALUE(r_instance) TYPE REF TO zcl_singleton_class. 

PRIVATE SECTION. 
types:
TY_CONNECTION_LIST TYPE STANDARD TABLE OF SPFLI WITH  KEY carrid connid.

class-data instance type ref to zcl_singleton_class .
class-data CONNECTION_LIST type TY_CONNECTION_LIST . 
ENDCLASS.

CLASS zcl_singleton_class IMPLEMENTATION.

method CLASS_CONSTRUCTOR.
instance = instance.
SELECT * FROM SPFLI INTO TABLE CONNECTION_LIST.
endmethod.

METHOD get_instance. 
r_instance = instance. 
ENDMETHOD.  
ENDCLASS.


CLASS ZCL_FLINFO DEFINITION. 

PUBLIC SECTION. 
CLASS-METHODS: 
CLASS_CONSTRUCTOR,
get_connection
    IMPORTING im_carrid type S_CARR_ID 
        RETURNING VALUE(re_connection) TYPE.
ENDCLASS.


CLASS ZCL_FLINFO IMPLEMENTATION.  

 METHOD get_connection. 
LOOP at CONNECTION_LIST TRANSPORTING NO FIELDS WHERE carrid = im_carrid.
  re_connection = re_connection + 1.
ENDLOOP. 
ENDMETHOD.  
ENDCLASS.

How can I implement the get_connection method of ZCL_FLINFO so it to iterate through the internal table CONNECTION_LIST of zcl_singleton_class, to count the number of connections for the given airline and return it in the parameter?


Solution

  • I figured out something the work out fine in my case. If a class A(zcl_singleton_class) offers friendship to another class B(ZCL_FLINFO), B can access the private components of A. So, I simply access the internal table(CONNECTION_LIST) by calling it in my loop.

    method GET_N_O_CONNECTIONS.
     LOOP AT zcl_singleton_class=>CONNECTION_LIST  TRANSPORTING NO FIELDS  WHERE  CARRID = IM_CARRID.
      RE_N_O_CONNECTIONS =  RE_N_O_CONNECTIONS + 1.
    ENDLOOP.
    endmethod.