Interface and Polymorphism in ABAP OOPS

Interface and Polymorphism in ABAP OOPS


Interface

The Interface is similar to class. Like class an interface also contains attributes, methods, and events but an interface contains only declarations, and the implementation of methods are done in the class which implements it.

  • Interface contains only method definition and no implementation.
  • The interface can be used by one or more classes, and this is known as polymorphism.
  • Interfaces are declared with the keyword INTERFACE.
  • Interfaces are implemented in a class using keyword INTERFACES and interface methods are implemented in the class which uses the interface.
  • Interfaces can be implemented only in the public section of a class
  • A class which uses an interface should implement all the methods of that interface else it will result in compilation error.
  • We cannot use default values using VALUE in interface attributes except constants. The values can be assigned only in class which uses the interface

Syntax for creating an interface in ABAP : 

INTERFACE <interface_name>. 
DATA..... 
CLASS-DATA..... 
METHODS..... 
CLASS-METHODS..... 
ENDINTERFACE.

To use the interface in any class, the interface must be declared in the DEFINITION part of class.

CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES: I1.
ENDCLASS.

Assume that interface I1 has method METHOD1, then the method can be implemented as.

CLASS c1 IMPLEMENTATION.
METHOD I1~METHOD1.
ENDMETHOD.
ENDCLASS.

Example Program:

REPORT ztest_program.

PARAMETERS: p_matnr TYPE matnr.

INTERFACE lcl_intf.

  METHODS: get_matnr_details
    IMPORTING iv_matnr TYPE matnr
    EXPORTING es_mara  TYPE mara.

  METHODS: get_matnr_description
    IMPORTING iv_matnr TYPE matnr
    EXPORTING es_makt  TYPE makt.
ENDINTERFACE.

CLASS lcl_material DEFINITION.

  PUBLIC SECTION.
    INTERFACES: lcl_intf.
ENDCLASS.

CLASS lcl_material IMPLEMENTATION.

  METHOD lcl_intf~get_matnr_details.

    SELECT SINGLE * FROM mara
      INTO es_mara
     WHERE matnr = iv_matnr.
  ENDMETHOD.

  METHOD lcl_intf~get_matnr_description.

    SELECT SINGLE * FROM makt
      INTO es_makt
     WHERE matnr = iv_matnr.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA(lo_obj) = NEW lcl_material( ).

lo_obj->lcl_intf~get_matnr_details(
  EXPORTING
    iv_matnr = p_matnr
  IMPORTING
    es_mara  = DATA(ls_mara)
).

lo_obj->lcl_intf~get_matnr_description(
  EXPORTING
    iv_matnr = p_matnr
  IMPORTING
    es_makt  = DATA(ls_makt)
).

WRITE:/ ls_mara-matnr, ls_mara-mtart, ls_mara-meins, ls_mara-matkl,
      / ls_makt-matnr, ls_makt-maktx.

Polymorphism using Interfaces

Polymorphism means many forms.

In the below program the interface “INTF1” is implemented in two classes C1 and C2. The method M1 has different implementations in C1 and C2. By using the class references and assigning them to interface reference and calling the method M1 each time, the polymorphism concept can be achieved.

Example:

REPORT ztest_program.

INTERFACE intf1.
  METHODS : m1 .
ENDINTERFACE.

CLASS c1 DEFINITION. " Class C1
  PUBLIC SECTION.
    INTERFACES : intf1.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD intf1~m1.
    WRITE:/5 'Method m1 in c1'.
  ENDMETHOD.
ENDCLASS.

CLASS c2 DEFINITION. " Class C2
  PUBLIC SECTION.
    INTERFACES : intf1.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
  METHOD intf1~m1.
    WRITE:/5 'Method m1 in c2'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: lo_iref  TYPE REF TO intf1.

  DATA(lo_obj1) = NEW c1( ).
  DATA(lo_obj2) = NEW c2( ).

  lo_iref = NEW c1( ).  " C1 Ref
  lo_obj1->intf1~m1( ). " M1 Method call from C1

  lo_iref = NEW c2( ). " C2 Ref
  lo_obj2->intf1~m1( )." M1 Method call from C2

Output:

Method m1 in c1
Method m1 in c2

Post a Comment

Previous Post Next Post