Skip to Content
-2

If you do not access an interface using a class, you can only access its constants.

Hi friends,

as per Key word documentation of INTERFACE - components provided by SAP

The only interface components that can be addressed without implementation of the interface in classes are data types and constants. The latter can be addressed using the name of the interface and the class component selector (=>).

but when i tried to access one of the interface component of type instance data type variable ,directly inside the class without including INTERFACES then i am getting error as

"If you do not access an interface using a class, you can only access its constants."


please find the sample code snippet that i am getting above error

INTERFACE inf.

  TYPES: BEGIN OF typ_vbak,
    vbln TYPE vbak-vbeln,
         END OF typ_vbak.
 DATA:lv_radious TYPE i.
 CONSTANTS: c_py TYPE p DECIMALS 2 VALUE '3.14'.
  ENDINTERFACE.



  CLASS lcl DEFINITION.
    PUBLIC SECTION.
    METHODS:m1.
    PRIVATE SECTION.
    DATA: lv_area TYPE p DECIMALS 2.
   ENDCLASS.

   CLASS lcl IMPLEMENTATION.
     METHOD m1.   
       DATA:ls_vbak TYPE inf=>typ_vbak.
       inf=>lv_radious = 10.
       lv_area = ( inf=>c_py * inf=>lv_radious * inf=>lv_radious ).
       WRITE: / 'Area=',lv_area.
       ENDMETHOD.
     ENDCLASS.


     START-OF-SELECTION.
     DATA: o_lcl TYPE REF TO lcl.
     CREATE OBJECT o_lcl  TYPE lcl.
     o_lcl->m1( ).

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Aug 16, 2018 at 07:20 AM

    You answered yourself in your question:

    "The only interface components that can be addressed without implementation of the interface in classes are data types and constants."

    With:

    inf=>lv_radious = 10.

    you are trying to access DATA component of interface without interface implementation in some class.

    EDIT: You probably intended to do something like this?

    INTERFACE inf.
      TYPES: BEGIN OF typ_vbak,
               vbln TYPE vbak-vbeln,
             END OF typ_vbak.
      DATA:lv_radious TYPE i.
      CONSTANTS: c_py TYPE p DECIMALS 2 VALUE '3.14'.
    ENDINTERFACE.
    
    CLASS lcl DEFINITION.
      PUBLIC SECTION.
        INTERFACES inf. "<< use interface inf in class lcl
        METHODS:m1.
      PRIVATE SECTION.
        DATA: lv_area TYPE p DECIMALS 2.
    ENDCLASS.
    
    CLASS lcl IMPLEMENTATION.
      METHOD m1.
        DATA:ls_vbak TYPE inf=>typ_vbak.
        inf~lv_radious = 10.
        lv_area = ( inf=>c_py * inf~lv_radious * inf~lv_radious ).
        WRITE: / 'Area=',lv_area.
      ENDMETHOD.
    ENDCLASS.
    
    *--------------------------------------------------------------------*
    START-OF-SELECTION.
      DATA: o_lcl TYPE REF TO lcl.
      CREATE OBJECT o_lcl TYPE lcl.
      o_lcl->m1( ).
    

    Add comment
    10|10000 characters needed characters exceeded