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

classes

Former Member
0 Likes
587

Hi All,

I want the differences between the public, private and protected classes with examples..Thanks in advance.

4 REPLIES 4
Read only

Former Member
0 Likes
549

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>

http://www.erpgenie.com/sap/abap/OO/eg.htm

Read only

uwe_schieferstein
Active Contributor
0 Likes
549

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

Read only

Former Member
0 Likes
549

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