Showing results for 
Search instead for 
Did you mean: 

Best practice for "How to use Interface attributes"?

Galactic 2
Galactic 2

Hey there!

Currently I am struggeling on how - and if - to use attributes in a class that uses an interface.

In the past - when I designed an object - I used an interface and added the attributes I needed. Then a colleague stated that Interfaces should not have attributes for the following reasons:

  1. all attributes will be PUBLIC
  2. the implementing class should define by itself which attributes it uses 

I know that the opinions about PUBLIC attributes are widely spread. Some say, that a class should not have any PUBLIC attributes. All data accesses should be done by GETTER methods. Others (like me) state that PUBLIC attributes are easy and flexible to use. Attributes can be easily changed. If I use GET and SET methods, then I will have the following two options:

  1. read the data with GET method, store it in a local variable, and change the attribute by using the SET method.
  2. Provide a special method for a change of that specific field.

Option 1 makes manipulating the data of the object more difficult as it could be.

The 2nd option requires that the object knows in advance what data the caller will be going to change.

I understand that providing GETTER and SETTER methods for a class is useful because the object has full control of it's data. The data cannot be manipulated so that some data might get invalid. But in 20 years of ABAP programming I have not often experienced that it was necessary that an object has the only control over its data.

Quite contrary to the experience that I as a caller of classes could not do the things that could be possible if the objects data was PUBLIC.

btw: Access by GETTER method is 100-150% slower than direct access of PUBLIC attributes...

What are your experiences, your thoughts and opinions about this topic?