Interface - Type of ABAP Objects

Interface - Type of ABAP Objects

Let us suppose we have two abstract classes, class A & class B. Now we know that the abstract class must contains at least one abstract method. Hence more than one abstract classes (here A & B) have abstract methods in each of those classes.

Now we want to implement those abstract methods. To do that we need to create a subclass of those abstract classes A & B. So a new class C will be created which is a subclass of A & B. This situation is called multiple inheritances. Unfortunately ABAP doesn’t support multiple inheritances.

To use this multiple inheritances functionality we need to use Interface.

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 relation is built by interface to class it needs implements.

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 it.

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

Moreover we can refer the interface with the normal class. This is because to refer something there is no need to create object. Let’s take the example of JAVA.

We have interfaces A & B and we have normal class C.
To instantiate the class C: C obj = new C( );
Here we are defining the default constructor C( ). But this will not happen for creating object of interface. Here we can refer the interface with the normal class C like:
A objA = new C( );
B objB = new C( );
This is possible because we are using the memory of constructor C to create the reference of interface. It will work as a normal object. But this reference will have no access of class C’s methods. It can only use its own declared methods.

In the following program we have declared two interfaces i_a & i_b. Each of those contain 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 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

Whole program is as follows.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       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.  DATA obj TYPE REF TO cl_c.
  CREATE OBJECT obj.

  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.

The output is as follows:

Share this

Related Posts