SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Object Oriented ABAP - Local Class can understand data and types in the global area of the program

Object Oriented ABAP - Local Class can understand data and types in the global area of the program

This program will demonstrate the following:-

  • Different attributes of a class can be constructed utilizing the data and types declared outside the class, in the global area of the program.
  • Data declared in the global area of the program can be used directly in a class.

The global section of this program contains a type : TYP_TAB and an integer variable , NUM1.

These type and data are used while defining attributes L_NUM1(integer) and IT_TAB (internal table) for class C1 . Also, the global data L_NUM is used directly inside the program.

 



REPORT zstp_oo_abap.

TYPES : BEGIN OF typ_tab ,
          name(15) TYPE c,
          age      TYPE i,
        END OF typ_tab .

DATA : num1 TYPE i VALUE 5 .

CLASS c1 DEFINITION .
  PUBLIC SECTION.
    METHODS : meth1 .
    DATA : l_num  LIKE num1,
           it_tab TYPE STANDARD TABLE OF typ_tab,
           w_tab  LIKE LINE OF it_tab.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD : meth1 .
    DATA : l_cnum(2) TYPE c.
    l_num = 0.
    DO 5 TIMES.
      l_num = l_num + 1.
      l_cnum = l_num.
      CONCATENATE 'Student-'
                  l_cnum
                  INTO w_tab-name.
      w_tab-age = num1 * l_num .
      APPEND w_tab TO it_tab.
      CLEAR w_tab.
    ENDDO.
    LOOP AT it_tab INTO w_tab.
      WRITE:/5 w_tab-name ,
               w_tab-age.
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.
  DATA : obj1 TYPE REF TO c1.

  CREATE OBJECT : obj1.
  CALL METHOD obj1->meth1.

 

Output

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in