Mentor SAP
2017-07-13 Submitted by:- Admin

Let us suppose we have two interfaces (A & B) rather than the classes. Now we can create a class C which implements these interfaces. When the relation is built by class to class, it needs inheritance. But when the relationship is built on the interface to class it needs implementation.

 

Now in this C class, we implement all the abstract methods which have been declared in interfaces (A & B). After that, we create an object of class C and then we can use this object to trigger different method of interface or class itself.

 

Most important point is that we can’t instantiate an interface. It means we can’t have implementation anything (like methods, constructor etc.) in the interface. So to access the interface we need to create a normal class which implements those interfaces and then we can instantiate it.

 

In the following program, we have declared two interfaces i_a & i_b. Each of those contains own data and methods. Now we are declaring a normal class where we define normal data & methods. Then we declare the interfaces.

 

INTERFACES: i_a,
                                i_b.

 

At the implementation point, we define all the methods (own method and interface methods also). The interface method is defined like this.

 

  METHOD i_a~m_inta.
    i_a~v_inta = 'I am from A Interface method'.
    WRITE: /3 i_a~v_inta.
  ENDMETHOD.                    "i_a~m_inta

 

After the start of selection at the time of calling methods, we need to call by this way.

 

  CALL METHOD: obj->m_test,          "Calling normal method
                                       obj->i_a~m_inta, "Calling interface method
                                      obj->i_b~m_intb. "Calling interface method

REPORT zinterface_multiple.
*----------------------------------------------------------------------*
*       INTERFACE i_a
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE i_a.
  DATA v_inta TYPE char40.
  METHODS m_inta.
ENDINTERFACE.                    "i_a

*----------------------------------------------------------------------*
*       INTERFACE i_b
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE i_b.
  DATA v_intb TYPE char40.
  METHODS m_intb.
ENDINTERFACE.                    "i_b

*----------------------------------------------------------------------*
*       CLASS cl_c DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_c DEFINITION.
  PUBLIC SECTION.
    DATA v_test TYPE char40.
    METHODS m_test.
    INTERFACES: i_a,
      i_b.
ENDCLASS.                    "cl_c DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_c IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_c IMPLEMENTATION.
  METHOD m_test.
    v_test = 'This is normal method'.
    WRITE: /3 v_test.
  ENDMETHOD.                    "m_test

  METHOD i_a~m_inta.
    i_a~v_inta = 'I am from A Interface method'.
    WRITE: /3 i_a~v_inta.
  ENDMETHOD.                    "i_a~m_inta

  METHOD i_b~m_intb.
    i_b~v_intb = 'I am from B Interface method'.
    WRITE: /3 i_b~v_intb.
  ENDMETHOD.                    "i_b~m_intb
ENDCLASS.                    "cl_c IMPLEMENTATION

*Start of selection
START-OF-SELECTION.

* Data Declaration
  DATA obj TYPE REF TO cl_c.

* Create object
  CREATE OBJECT obj.

* Call method
  CALL METHOD: obj->m_test,     "Calling normal method
               obj->i_a~m_inta, "Calling interface method
               obj->i_b~m_intb. "Calling interface method
  SKIP.
  "Executing interface data
  WRITE: /3 'Interface Data: ', obj->i_b~v_intb.

 

Output