Interface is declared in Public Section of Class

Interface is declared in Public Section of Class


Interface can only be declared in the public section of any class. Any other section will cause syntax error in the system.

Here is a program where we have declared an Interface it. We have defined attribute and method in this interface. Next we have defined class where we have declared the interface in the protected section. After implementing the method we compile the program and a syntax error has come as follows. 



Below is another version of the program where we have commented the protected section. And this time the program compiles successfully as produce the proper output.

*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       INTERFACE it
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE it.
  DATA v_txt TYPE char50.
  METHODS meth.
ENDINTERFACE.                    "it

*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
  PUBLIC SECTION.
    DATA date TYPE sy-datum.
    INTERFACES it.
ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
  METHOD it~meth.
    date = sy-datum.
    WRITE: / it~v_txt,
           / 'Today''s Date is', date DD/MM/YYYY.
  ENDMETHOD.                    "it~meth
ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO cls.
  CREATE OBJECT obj.

  obj->it~v_txt = 'SAP ABAP Object Oriented'.
  CALL METHOD obj->it~meth.


The output is following:

Share this

Related Posts