Skip to main content

Interfaces and Polymorphism

IntermediateModularization & OOP⏱ 25 min

Task

Implement an interface and demonstrate polymorphism using different class implementations.

Write Your ABAP Code

ABAP Editor
Loading...
📥 Sample Input & Output
Vehicle Type: Car
Output: Driving a car
💡 Hint

Program to an interface, not to a concrete class.

✅ View Reference Solution
INTERFACE lif_vehicle.
  METHODS drive RETURNING VALUE(rv_text) TYPE string.
ENDINTERFACE.

CLASS lcl_car DEFINITION.
  PUBLIC SECTION.
    INTERFACES lif_vehicle.
ENDCLASS.

CLASS lcl_car IMPLEMENTATION.
  METHOD lif_vehicle~drive.
    rv_text = 'Driving a car'.
  ENDMETHOD.
ENDCLASS.