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

Outdated question about a temporary bug on ADT

simonegaffurini
Participant
0 Likes
1,109

This question was a temporary bug of ADT.

This question was a temporary bug of ADT.

3 REPLIES 3
Read only

BiberM
Contributor
0 Likes
934

In my opinion your first implementation should be preferred as it reduces the member access. Pay attention that you cannot write your member from outside as it is declared read only.

What should work:

data(lo_instance) = new zcl_x( ).

write lo_instance->my_interface_string.
Read only

0 Likes
934

doing so in eclipse, I get my_interface_string with an underline and the error message "Field "MY_INTERFACE_STRING" is unknown."

Read only

FredericGirod
Active Contributor
0 Likes
934

At first, an Interface is a contract between a class & the outside.

This contract is here to expose a set of public methods regarding the same logic.

A class should use one or several interfaces to exchange with the outside (using public method in class is bad)

So now, looking your code, what is the responsibility of your interface ? store value ? an interface is not instantiable. So why trying to do this ?

Your interface, should have setter & getter method, and not the way to store the value, This is the responsibility of the class.

Nevertheless, the answer of your question is:

INTERFACE zif_x.
DATA: my_interface_string TYPE string. " READ-ONLY.
ENDINTERFACE.



CLASS zcl_x DEFINITION
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES zif_x.
METHODS: constructor.
ENDCLASS.

CLASS zcl_x IMPLEMENTATION.
METHOD constructor.
zif_x~my_interface_string = 'hello'.
ENDMETHOD.
ENDCLASS.



START-OF-SELECTION.

DATA lo_ref TYPE REF TO zif_x.
lo_ref = NEW zcl_x( ).
write lo_ref->my_interface_string.



DATA lo_ref2 TYPE REF TO zcl_x.
lo_ref2 = NEW #( ).
write lo_ref2->zif_x~my_interface_string.