‎2007 Jan 22 8:04 AM
Hi All,
I want the differences between the public, private and protected classes with examples..Thanks in advance.
‎2007 Jan 22 8:17 AM
Hi vijay,
1. <b>Public attributes</b>
Public attributes are defined in the PUBLIC section and can be viewed and changed from outside the class. There is direct access to public attributes. As a general rule, as few public attributes should be defined as possible.
PUBLIC SECTION.
DATA: Counter type i.
<b>Public methods</b>
Can called from outside the class
PUBLIC SECTION.
METHODS: set_attributes IMPORTING p_name(25) TYPE c,
p_planetype LIKE saplane-planetyp,
2. <b>Private attributes</b>
Private attributes are defined in the PRIVATE section. The can only be viewes and changed from within the class. There is no direct access from outside the class.
PRIVATE SECTION.
DATA: name(25) TYPE c,
planetype LIKE saplane-planetyp,
<b>Private methods</b>
Can only be called from inside the class. They are placed in the PRIVATE section of the class.
3.<b>Protected components</b>
When we are talking subclassing and inheritance there is one more component than Public and Private, the Protected component. Protected components can be used by the superclass and all of the subclasses. Note that Subclasses cannot access Private components.
<b>Example</b>
‎2007 Jan 22 8:23 AM
Hello Vijay
<b>Public</b> classes can, obviously, instantiated by everybody.
Example: CL_GUI_ALV_GRID
For instantiating <b>protected</b> or <b>private</b> classes you will need a static CREATE method.
Example: CL_RECA_MESSAGE_LIST (created by calling CF_RECA_MESSAGE_LIST=>CREATE( ) ).
You will use the second approach if you want to specifically control the instantiating of your class.
Regards
Uwe
‎2007 Jan 22 9:28 AM
Refer this link
http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm
http://help.sap.com/saphelp_nw04/helpdata/en/b3/f4b1406fecef0fe10000000a1550b0/content.htm
Message was edited by:
Judith Jessie Selvi
‎2007 Feb 13 6:56 PM
Hi Vijay,
The class visibility refers to its instantiation: CREATE OBJECT.
As already mentioned in other replies but in a different way:
Public: CREATE OBJECT can be called from anywhere
Protected: CREATE OBJECT can be called from the class and its subclasses
Private: CREATE OBJECT can only be called from within the class
Abstract: CREATE OBJECT cannot be called at all.
Of course, I mean CREATE OBJECT <i>variable_type_ref_to_the_class</i>.
Private instantiation is typically used for the SINGLETON pattern where you allow only one instance of your class to exist. In such case, I always create a static method GET_INSTANCE() which returns the singleton instance of my class.
Abstract instantiation is used when a class represents an interface with a base implementation.
Protected instantiation is less common and used in particular cases.
Best regards,
Simon