Monday, 19 September 2016

Global Class : Friends

Ex :-

  • If a class A declares itself as a friend to class B then class B will have access to all the private and protected attributes and methods of class A. 

Steps :-

Class A

  • Go to Transaction code SE24.
  • Enter the class name which start's with ZCL_<Class name > and click on Create.
  • Enter the method name , Level and Visibility.
  • To pass the parameters , select the method name and click on Parameters.
  • Pass the parameters.



  • Click on Save button and click on Back.
  • To implement the method , double click on method name.
  • Implement the method "Fetch_data ".
  • Save -> Check .
  • Click on Back button.
  • To pass the parameters to "DISPLAY_DATA" method , select method name and click on Parameters.




  • Pass the parameters.

  • Click on Save and click on Back button.
  • To implement the method , double click on method name.

  • Implement it.

  • Save->Check.
  • Click on Back button.
  • Click on Friends Tab and enter the friend class name.

Note : Before activating Class ZCL_A , just create ZCL_B and activate it.
  • Save -> Check -> Activate.
  • Copy the class name.
Class B
  • Enter the class name which start's with Y or ZCL_< class name > and click on Create.
  • Click on Attribute tab and create a Attributes.


  • Enter the method name , Level and Visibility.
  • To pass the parameters , select the method name and click on Parameters.


  • Pass the parameters.
  • Click on SAVE and click on Back button.
  • To implement the method , double click on Method name.

  • Implement it.
DATA : OBJ_A  TYPE REF TO ZCL_A.

CREATE OBJECT OBJ_A.

  SELECT SINGLE *  FROM VBRK INTO EX_WA WHERE VBELN = IM_VBELN.


CALL METHOD OBJ_A->FETCH_DATA
  EXPORTING
    IM_VBELN =  IM_VBELN
  CHANGING
    CH_ITAB  = IT_VBRP
    .

  • Save -> Check and click on Back button.
  • Select the method name "DISPLAY_HEAD " and click on Parameters.

  • Pass parameters.
  • Click on Save and click on Back button.
  • Double click on Method name , to implement it.

  • Implement it.
WRITE : /20  'BILLING HEADER DATA ' COLOR 3.

WRITE : / IM_WA-VBELN , IM_WA-ERDAT , IM_WA-ERNAM , IM_WA-VKORG ,
            IM_WA-KUNRG.

ULINE.

DATA : OBJ_A TYPE REF TO ZCL_A.

CREATE OBJECT OBJ_A.

CALL METHOD OBJ_A->DISPLAY_DATA
  EXPORTING
    IM_ITAB = IT_VBRP
    .
  • Save -> Check.
  • Click on Back button.
  • Save -> Check -> Activate.
  • Copy the Class name.

Main Program

  • Go to Transaction Code SE38.
  • Enter the program name which start's with Y or Z< program name > and click on Create.

Source Code


DATA : OBJ TYPE REF TO ZCL_B.

DATA : WA TYPE VBRK.

PARAMETERS : P_VBELN TYPE VBRK-VBELN.

START-OF-SELECTION.

CREATE OBJECT OBJ.

CALL METHOD OBJ->FETCH_HEAD
  EXPORTING
    IM_VBELN =  P_VBELN
  IMPORTING
    EX_WA    = WA
    .


CALL METHOD OBJ->DISPLAY_HEAD
  EXPORTING
    IM_WA  = WA
    .

  • Save -> Check -> Activate.
  • Execute.


Input

  • Enter the input and click on Execute ( F8 ).



Output

  • Click on Execute.


1 comment: