Reuse a true “is a” relationship carefully

The dispatch team charges a standard fee and has a faster delivery option. If express delivery really is a kind of delivery with a substitutable fee rule, inheritance can share the common contract and redefine the variable behavior.

Base and specialized class

CLASS lcl_delivery DEFINITION.
  PUBLIC SECTION.
    METHODS fee RETURNING VALUE(result) TYPE i.
ENDCLASS.

CLASS lcl_delivery IMPLEMENTATION.
  METHOD fee.
    result = 5.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_express_delivery DEFINITION INHERITING FROM lcl_delivery.
  PUBLIC SECTION.
    METHODS fee REDEFINITION.
ENDCLASS.

The subclass inherits eligible components and explicitly marks the method it replaces with REDEFINITION. In this example it returns a different fee:

METHOD fee.
  result = 12.
ENDMETHOD.

Inheritance is not the default reuse tool

Inheritance models a specialization relationship and couples a subclass to its superclass. Use it when subclasses can honor the base class’s promises. If a service merely uses a policy or notifier, composition is usually a better fit. Avoid deep class trees built only to share a few lines.

Practice

Implement a base fee and redefine it for express delivery. Instantiate the express class and assign the method return to result; add the selected environment’s output. Structural check only—no ABAP runtime.

Version note: Inheritance restrictions and supported syntax may vary by target release/language version; ABAP Cloud also constrains APIs based on released-object rules.

Sources

Developer practice · ABAP

Practice: redefine an express-delivery rule

Define a base delivery class with a fee method, redefine it in an express subclass, instantiate the subclass, and assign its fee to result.

Choose your ABAP environment

This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.

Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.

0 required structures detected · 0 checks