Hello,

Sign up to join our community!

Welcome Back,

Please sign in to your account!

Forgot Password,

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

SAP EWM Help Latest Questions

  • 0
  • 0
DPM125

Polymorphism in SAP ABAP

Polymorphism allows different classes to be treated through a common parent reference, while still calling their own version of a method at runtime.

  • When to use it:
    When you want to treat multiple objects generically, but still get their specific behavior.
  • In frameworks or handlers where object types may vary.
  • In real-world modeling (e.g., Animal → Dog/Cat).

 

  •  Key Notes:
    Requires inheritance and method overriding.
  • Achieved using parent class references or interfaces.
  • Great for writing extensible and clean code.
REPORT zrep_lclclass.

CLASS cl_animal DEFINITION.
  PUBLIC SECTION.
    METHODSspeak.
ENDCLASS.

CLASS cl_animal IMPLEMENTATION.
  METHOD speak.
    WRITE'Animal sound'.
  ENDMETHOD.
ENDCLASS.

CLASS cl_dog DEFINITION INHERITING FROM cl_animal.
  PUBLIC SECTION.
    METHODSspeak REDEFINITION.
ENDCLASS.

CLASS cl_dog IMPLEMENTATION.
  METHOD speak.
    WRITE'Dog barks'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATAlo_animal TYPE REF TO cl_animal.

  lo_animal NEW cl_dog).
  lo_animal->speak).  " Outputs: Dog barks (runtime polymorphism) 

Output:

Related Questions

Leave an answer

Leave an answer