SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

OO ABAP - Simple use of an Interface

OO ABAP - Simple use of an Interface

This program will show simple use of an interface with its own data and methods and how it is implemented in a class. It will also show that there can be methods of same name for an interface and the class implementing the interface.

 

This program contains an interface I1 with attribute : NUM an method : METH1.

This interface is implemented in a class : C1 which also has its own method METH1.

An object OREF is created from class C1 and both the methods METH1 , one for class and another for interface is called using the object.


INTERFACE i1.
  DATA    : num TYPE i .
  METHODS : meth1.
ENDINTERFACE.

CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS : meth1.
    " class c1’s own method
interfaces : i1.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD : meth1.
    WRITE:/5 'I am meth1 in c1'.
  ENDMETHOD.

  METHOD i1~meth1.
    WRITE:/5 'I am meth1 from i1'.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  DATA : oref TYPE REF TO c1.
  CREATE OBJECT oref.
  WRITE:/5 oref->i1~num.
  CALL METHOD oref->meth1.
  CALL METHOD oref->i1~meth1.

 

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in