Google Search - Blog...........

ABAP - OOPS : Polymorphism Via Interfaces.

REPORT YSUBDEL.

interface I1.
METHODS : M1 .
ENDINTERFACE.

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

CLASS C1 IMPLEMENTATION.
METHOD I1~M1.
WRITE:/5 'I am method m1 in c1'.
ENDMETHOD.
ENDCLASS.

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

CLASS C2 IMPLEMENTATION.
METHOD I1~M1.
WRITE:/5 'I am method m1 in c2'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2 ,
IREF TYPE REF TO I1 .
CREATE OBJECT : OREF1 ,
OREF2 .
IREF = OREF1.
CALL METHOD IREF->M1.
IREF = OREF2.
CALL METHOD IREF->M1.


Output:

I am method m1 in c1
I am method m1 in c2

No comments:

Post a Comment