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

read only Attributes

rainer_hbenthal
Active Contributor
0 Likes
2,418

Hi,

i defined some attributes in a class, their visibility is protected. Now i wanted them to have as constants to avoid unwanted changes (by accident) and set the read only attribute. But when compiling the class the read only flag is being taken out without any further notice.

Is read only only available fpr public aatributes? Why not for protected attributes?

1 ACCEPTED SOLUTION
Read only

Clemenss
Active Contributor
0 Likes
1,953

Hi Rainer,

if you want them to be constants, define them as constants.

I think the Read-Only-Flag is just a crutch. Attributes should never be public. If they should not be changed from outside, no public SET method should exist.

Regards,

Clemens

8 REPLIES 8
Read only

Former Member
0 Likes
1,953

Hi Rainer,

The Read Only attribue can only be used for public attributes of the class. This is because the public attribute are accessible to all users of the class. So by using Read Only , the attribute can only be modified by the methods of the class or its subclasses.

Since protected or private attribute already possess this functionality , so they can not be specified as Read Only.

Also specifying Read Only doesnt mean that no body can change that attribute. It means that only methods of the class or subclass can change it.

(If you requirement is to prhobit any changes to an attribute , better use the concept of Final method).

Hope this help in your doubt.

Thanks

Gaurang Dixit

Read only

AnjaneyaBhardwaj
Contributor
0 Likes
1,953

Hi Poster ,

You need to understand that constants and attributes are two different thing . You can make a public attribite ready only to avoid changing .The READ-ONLY addition means that a public attribute that was declared can be read from outside, but can only be changed by methods in the same class. You can currently only use the READ-ONLY addition in the public visibilility section (PUBLIC SECTION) of a class declaration or in an interface definition.

Howevr you have the option of assigning one of the three levels to an attribute and they are

The declaration type specifies whether the attribute is an instance attribute ( DATA)

a static attribute ( CLASS-DATA)

or a constant ( CONSTANTS).

This field is adjacent to attribute field in parameter of a methode .

Hope this helps .

Read only

naimesh_patel
Active Contributor
0 Likes
1,953

Yes, READ-ONLY only applies to Public Attributes. This only serve its purpose when it is being used outside of the class. From Keyword help:

This addition is solely possible in the public visibility area of a class or in an interface. It has the effect that an attribute declared using DATA is readable from an external point, but can only be changed using methods of the class or its subclasses.

http://help.sap.com/abapdocu_70/en/ABAPDATA_OPTIONS.htm#&ABAP_ADDITION_2@2@

For Protected attributes, as per the principle all the subclasses would be able to use them. So, READ-ONLY is irrelevant.

If you want to make some protected attributes kind of READ-ONLY, you should do like this:

Define the attribute as Private.

Create a GET method as Protected. In the implementation, send the attribute value back to caller.

Use this GET Method when you want to refer this attribute.

Regards,

Naimesh Patel

Read only

0 Likes
1,953

Naimesh,

I am just curious. If we consider this statement...

It has the effect that an attribute declared using DATA is readable from an external point

...what you suggested seems not fully compatible with the above.

Having the attribute as private is a must in order subclass can't access it directly - will have to use protected superclass setter to change its value. But how about getter method. In order above rule could be fulfilled ("readable from extrernal point") shouldn't it be a public method, not protected one?

Thanks

Marcin

Read only

0 Likes
1,953

Marcin,

What I have suggested it achieve kind of READ-ONLY for the Attributes which I don't want my Subclasses to update. For that reason the attribute is private, GETTER method is protected. I will call this getter within my subclass to get the value of the attribute but Since I don't create SETTER, my subclass could not maintain this attribute.

I can directly achieve the READ-ONLY, if my attribute is PUBLIC, than why should I propose to have GETTER method

Regards,

Naimesh Patel

Read only

0 Likes
1,953

Thanks, but I am still confused

What I have suggested it achieve kind of READ-ONLY for the Attributes which I don't want my Subclasses to update. For that reason the attribute is private,

that's absolutely fine

GETTER method is protected

but this point is interesting to me. The GETTER should be public in order we could access the attrubute as READ-ONLY from outside the class (both user and subclass level).

I can directly achieve the READ-ONLY, if my attribute is PUBLIC, than why should I propose to have GETTER method

Having tha attribute public is irrelevant here as it wouldn't be READ-ONLY anymore and wouldn't need GETTER method (as you already pointed above). The thing is just my eagerness to know why you chose protected GETTER over a public one to achieve READ-ONLY behaviour for an attribute.

Sorry for being anoyning, but I just would like to know the reason. Maybe there is one I am not aware of:)

Regards

Marcin

Read only

0 Likes
1,953

Sorry to make you confuse.

I proposed the solution just a reduced flavor of READ-ONLY - Specific for Protected Attributes. This is not a replacement of the Public READ-ONLY feature.

Regards,

Naimesh Patel

Read only

Clemenss
Active Contributor
0 Likes
1,954

Hi Rainer,

if you want them to be constants, define them as constants.

I think the Read-Only-Flag is just a crutch. Attributes should never be public. If they should not be changed from outside, no public SET method should exist.

Regards,

Clemens