SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

OO ABAP–Change/Display public attributes of a class from outside

OO ABAP–Change/Display public attributes of a class from outside



CLASS parentclass DEFINITION .
  PUBLIC SECTION.
    DATA : commondata(30) TYPE c VALUE 'Accessible to all'.
    METHODS : showval.
  PROTECTED SECTION.
    DATA : protectdata(40) TYPE c VALUE 'Protected data'.
  PRIVATE SECTION.
    DATA : privatedata(30) TYPE c VALUE 'Private data'.
ENDCLASS.

CLASS parentclass IMPLEMENTATION.
  METHOD : showval.
    WRITE:/5 'All data from parentclass shown:-'.
    WRITE:/ sy-uline.
    WRITE:/5 commondata,
          /5 protectdata,
          /5 privatedata.
  ENDMETHOD.
ENDCLASS.

CLASS childclass DEFINITION INHERITING FROM parentclass.
  PUBLIC SECTION .
    METHODS : subval.
ENDCLASS.

CLASS childclass IMPLEMENTATION.
  METHOD : subval.
    SKIP 1.
    WRITE:/5 'Data of parent shown from child-'.
    WRITE:/5 sy-uline.
    WRITE:/5 commondata,
          /5 protectdata.
    commondata = 'Public data changed in subclass'.
    protectdata = 'Protected data changed in subclass'.
    WRITE:/5 sy-uline.
    WRITE:/5 commondata,
          /5 protectdata.
  ENDMETHOD.
ENDCLASS.



START-OF-SELECTION.
  DATA : parent TYPE REF TO parentclass ,
         child  TYPE REF TO childclass .
  CREATE OBJECT : parent ,
                  child  .
  CALL METHOD : parent->showval ,
                child->subval.
  SKIP 2.
  parent->commondata = 'User changing public data'.
  WRITE:/5 parent->commondata.

Output -

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in