05-08-2008 12:07 PM
how to access the protected class in OOABAP..can any one help me with sample code
05-08-2008 1:13 PM
Hi,
Data declared in the protected section can be accessed by the class itself, and also by its subclasses but not by external users outside the class.
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
All data from parentclass shown:-
Accessible to all
Protected data
Private data
Data of parent shown from child-
Accessible to all
Protected data
Public data changed in subclas
Protected data changed in subclass
User changing public data
If access outside of class
-
CLASS c1 DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value 'Accessible to all'.
PROTECTED SECTION.
DATA : protectdata(40) type c value 'Protected data'.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
DATA : obj1 type ref to c1.
create object : obj1.
write:/5 obj1->protectdata ,
obj1->privatedata.
it gives compilation error will be generated which will prove that protected and private components of a class cannot be accessed by external users.
*Reward points.
05-08-2008 1:20 PM
What is it that you want to access? The static methods and attributes can be access without any problems like this:
DATA: gc_formfield TYPE string.
gc_formfield = cl_http_server=>co_form_field.
But this is only possible if the attribute is public, otherwise you will have to create an instance of the class as well to access it.
If, however, you want to access the instance attributes you will have to create the instance first like this:
REPORT zzz_test3.
*----------------------------------------------------------------------*
* CLASS lcl_user DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_user DEFINITION INHERITING FROM cl_user_defaults CREATE PUBLIC.
PUBLIC SECTION.
DATA: gr_user TYPE syuname.
ENDCLASS. "lcl_user DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_user IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_user IMPLEMENTATION.
ENDCLASS. "lcl_user IMPLEMENTATION
START-OF-SELECTION.
DATA: gr_defaults TYPE REF TO lcl_user,
gt_defaults TYPE usrdflt_itab,
wa_defaults LIKE LINE OF gt_defaults.
CREATE OBJECT gr_defaults
EXPORTING
iv_calling_program = sy-repid.
gt_defaults = gr_defaults->get( ).
LOOP AT gt_defaults INTO wa_defaults.
ENDLOOP.
Nothing really happens here, but I just want to show you how you instantiate this class and have access to the instance methods and attributes.