‎2007 Dec 20 11:24 AM
Hi all,
Below is my code :
<code>
class c1 definition.
protected section.
methods : meth1.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
class c1 implementation.
method meth1.
write : / 'hi i am in meth1'.
write 😕 sy-uline.
write 😕 privatedata.
endmethod.
endclass.
start-of-selection.
data : obj type ref to c1.
create object: obj.
call method : obj->meth1.
</code>
Frnds like i wanted to have the exact usage of Private , Protected section.
Here i know the definition
Private section : visible only to the class.
Protected : visible to class and its subclass.
So here in my example C1 is the same class so why we are unable to access "PRIVATE" and "PROTECTED".
Thanks,
satish
‎2007 Dec 20 11:34 AM
Hi,
Please check below...
Public Section
All of the components declared in the public section are accessible to all users of the class, and to the methods of the class and any classes that inherit from it. The public components of the class form the interface between the class and its users.
Protected Section
All of the components declared in the protected section are accessible to all methods of the class and of classes that inherit from it. Protected components form a special interface between a class and its subclasses. Since inheritance is not active in Release 4.5B, the protected section currently has the same effect as the private section.
Private Section
Components that you declare in the private section are only visible in the methods of the same class. The private components are not part of the external interface of the class.
The components of a class can only be accessed through the objects but declaring the class
components in different domains, which are defined as Public Private and Protected, these
domains control the accessibility.
Public components of a class can be accessed by the objects of a class components in the
public domain are totally visible outside the class other components within the class can also
access them.
Private components cannot be accessed by the objects of the class they can only be accessed
by the members within the class i.e. any member function within the class can access the
private members thus from the point of visibility no private components are visible outside
class. The sequence of declaring the components are public protected and private.
Example
CLASS my_class DEFINITION.
PUBLIC SECTION.
METHODS: test_method.
PROTECTED SECTION.
PRIVATE SECTION.
DATA: name TYPE C.
ENDCLASS.
CLASS my_class IMPLEMENTATION.
METHOD test_method.
Write:/ this is my test method.
ENDMETHOD.
ENDCLASS.
Regds
Sivaparvathi
Please reward points if helpful.....
‎2007 Dec 20 11:35 AM
Hi Satish,
private means that you can only access the attribute/method within the class. So define an public method in your class which access the private methods or attributes.
With protected it is the same, but if you inherite from this class, the subclass can also use access you method/attribute.
You meth1 is protected, so it can't be called from outside (from your obj).
Cheers,
Stefan.
‎2007 Dec 20 11:53 AM
Satish,
see below points on visibility sections from SAP help. According that Protected section is not available to all users.
Public Section
All of the components declared in the public section are accessible to all users of the class, and to the methods of the class and any classes that inherit from it. _The public components of the class form the interface between the class and its users._
Protected Section
All of the components declared in the protected section are accessible to all methods of the class and of classes that inherit from it. Protected components form a special interface between a class and its subclasses. Since inheritance is not active in Release 4.5B, the protected section currently has the same effect as the private section.
Private Section
Components that you declare in the private section are only visible in the methods of the same class. The private components are not part of the external interface of the class.
‎2007 Dec 20 1:29 PM
Hi, Satish!
Here is the change to your code which will make it work.
<code>
class c1 definition.
public section.
methods : meth1.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
class c1 implementation.
method meth1.
write : / 'hi i am in meth1'.
write 😕 sy-uline.
write 😕 privatedata.
endmethod.
endclass.
start-of-selection.
data : obj type ref to c1.
create object: obj.
call method : obj->meth1.
</code>
Or, you can do the following as Stefan suggested:
<code>
class c1 definition.
public section.
methods: meth0.
protected section.
methods : meth1.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
class c1 implementation.
method meth0.
call method meth1.
write privatedata.
endmethod.
method meth1.
write : / 'hi i am in meth1'.
write 😕 sy-uline.
write 😕 privatedata.
endmethod.
endclass.
start-of-selection.
data : obj type ref to c1.
create object: obj.
call method : obj->meth0.
</code>
Good luck!
Vladan