Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

qustion in abap objects

Former Member
0 Likes
482

hallow

i wont to now what is the difference betwen public & private please give me

example of benefit of both.and when its recommended to use any of them

Regards

1 ACCEPTED SOLUTION
Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
435

Hi,

The public and Private section is one of the visibility sections of a

class, which determines the external visibility of the class

components.

All of the components defined in the public section are

visible in in same class or subclasses on protected and private sections.

All of the components defined in the public section are

visible out of the class or in subclasses.

DATA OBJ TYPE REF TO C1.

...

OBJ->A1 = 'X'.

The private section must be the last visibility section to be defined (after the public and

protected sections).

All of the components defined in the private section are

visible only in the same class.

You can't do it:

DATA OBJ TYPE REF TO C1.

...

OBJ->A1 = 'X'.

Or acess in the subclass.

Regards.

Marcelo Ramos

3 REPLIES 3
Read only

Former Member
0 Likes
435

Public Class :

Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.

Private Class :

Data declared in the private section can be accessed by the class only, but not by its subclasses and by external users outside the class.

REPORT YSUBDEL LINE-SIZE 120.

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.

Thanks

Seshu

Read only

Former Member
0 Likes
435

Hi,

Public: It can be inherited or accessible to the subclasses.

Private: Which can't be access by any classes.

e.g.

class A

private: str

public:str1

class B inheriting A

object of class B can access str1 but cant access str.

Reward if useful!

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
436

Hi,

The public and Private section is one of the visibility sections of a

class, which determines the external visibility of the class

components.

All of the components defined in the public section are

visible in in same class or subclasses on protected and private sections.

All of the components defined in the public section are

visible out of the class or in subclasses.

DATA OBJ TYPE REF TO C1.

...

OBJ->A1 = 'X'.

The private section must be the last visibility section to be defined (after the public and

protected sections).

All of the components defined in the private section are

visible only in the same class.

You can't do it:

DATA OBJ TYPE REF TO C1.

...

OBJ->A1 = 'X'.

Or acess in the subclass.

Regards.

Marcelo Ramos