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

Class attribute adopted from interface - how to set it private

Former Member
0 Likes
2,957

Hi there OO-style ABAPers,

I've just begun an ABAP OO adventure and have such a small problem ...

... I created a class, let's say MyClass which has some private attributes (Let's say MyAttr1, MyAttr2). Now I'm Implementing an interface let's say MyIface which has an attribute MyIfaceAttribute.

But after implementing interface - MyIfaceAttribute is automatically set to public in MyClass and there is no possibility to edit it.

And now I've got a small mess:

private MyAttr1

private MyAttr2

public MyIfaceAttribute

... whilst I'd like to have them all private

How can I change MyIfaceAttribute accessibility implemented in MyClass into private ?

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
1,475

Hello Pjotr

Private attributes do not make sense in interfaces. In order to "protect" you public interface attribute from being modified externally just define the attribute as READ-ONLY.

If you want to provide MODIFY access to the attribute you need to add a SET_MYATTRIBUTE method to your interface.

Regards

Uwe

5 REPLIES 5
Read only

uwe_schieferstein
Active Contributor
1,476

Hello Pjotr

Private attributes do not make sense in interfaces. In order to "protect" you public interface attribute from being modified externally just define the attribute as READ-ONLY.

If you want to provide MODIFY access to the attribute you need to add a SET_MYATTRIBUTE method to your interface.

Regards

Uwe

Read only

0 Likes
1,475

Uwe Schieferstein

Private attributes do not make sense in interfaces.

Could You please tell me why? I'm a real newbie in object oriented way of thinking, what is the argument not to make private attribute in interface? I would be really thankful for explaining.

Greetings. P.

Read only

0 Likes
1,475

Hi Piotr,

Interfaces from definition are public and they do not distinguish any other visibility. One of the reason would be that when you implement this interface in your class, it must be visible in all its subsequent classes (inherintng from this class). For private attributes/methods it wouldn't be feasible, though for protected it would.

Therefore entire definition of interfaces is just inlcuded in public section of class implementing it.

Moreover the interfaces are mainly designed to behave polimorphically so using one reference variable you can call different methods implementation (interface methods). This must be available from outside of interface/class implementing it. That's why its definition must only be public.

Regards

Marcin

Read only

0 Likes
1,475

Hello Piotr,

in ABAP OO an interface is intended as well known API to deal with. Therefore it would contradictionary to hide elements of an interface. Or in other words if it should´t be public / used by others why should an element part of the interface. Just remove it from the interface and move it to the implementing class.

Best Regards

Klaus

Read only

naimesh_patel
Active Contributor
0 Likes
1,475

Once you implement the interface in your class, you can set the Aliases and the visibility of the component.

In SE24 (Global class builder) you would find the Aliases tab.

Here you can specify the "simple" name of the attribute and methods as well as you can specify the visibility.

Regards,

Naimesh Patel