Monday, April 1, 2013

Interface Static Attribute and Method are Accessed Directly


Static attribute and method of Interface can be accessed directly in the data processing as of class. There is no need to create object for class.

Following program contains an interface where we have defined constants, static data and static method. The program contains a class which defines the interface and implements its method. Now in the start of selection we are valuing the class data directly by cls=>it~cls_var = 'Interface Data valued directly' statement and calling the method directly by call method cls=>it~cls_mit statement.

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

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       INTERFACE it
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE it.
  CONSTANTS c_var TYPE char40 VALUE 'SAP ABAP Object Oriented'.
  CLASS-DATA cls_var TYPE char50.
  CLASS-METHODS cls_mit.
ENDINTERFACE.                    "it

*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
  PUBLIC SECTION.
    INTERFACES it.
ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
  METHOD it~cls_mit.
    WRITE: / 'Interface Method',
           / it~cls_var,
           / it~c_var.
  ENDMETHOD.                    "it~cls_mit
ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.
  cls=>it~cls_var = 'Interface Data valued directly'.
  CALL METHOD cls=>it~cls_mit.


The output is below:


1 comment:

Unknown said...

Thanks for the great information in your blog SAP Success FactorsTraining in Chennai