Application Development 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: 

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

Balu483
Participant
0 Kudos

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( ).

3 REPLIES 3

Tomas_Buryanek
Active Contributor

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( ).

-- Tomas --

0 Kudos

without mention INTERFACES inside the class can't we directly access the instance attribute of an interface,inside the class method implementation?

0 Kudos

ABAP documentation on Interfaces - this should help

-- Tomas --