Code examples · Modern ABAP · Intermediate
ABAP OO: an interface, inheritance and polymorphism with shipping methods
The problem
An order system calculates shipping cost for standard delivery, express delivery (twice the standard price) and pickup (free).
How we solve it
- 1Contractinterface
- 2Implementstandard, pickup
- 3Inheritexpress
- 4Usepolymorphism
The result
For a 10 kg parcel the three methods return their own price through the same call.
1. The requirement
An order system calculates shipping cost for standard delivery, express delivery (twice the standard price) and pickup (free). A new shipping method must be addable later without changing the code that calls them.
2. How we design it
The caller only knows an interface, ZIF_SHIPPING_METHOD, with a name and a cost. Each shipping method is its own class that implements the interface. Express delivery inherits from standard delivery and redefines only the cost, because it is the standard price times two. Pickup is independent. The caller keeps a list of interface references and calls each one without knowing the class behind it. This is polymorphism.
Why we decided it this way
- 1Program against an interfaceThe caller depends on the contract, not on a class. A fourth carrier needs a new class and no change in the caller.
- 2Inherit only where there is a real is-a relationshipExpress delivery really is standard delivery with a surcharge, so it reuses the parent price. Pickup shares nothing, so it implements the interface directly.
- 3Decimal floating point for costPrices with decimals should not be calculated in integers, where a division silently rounds.
Before you start
- Basic ABAP classes. In ADT create each class and the interface as a separate object.
3. The code, step by step
Step 1 of 4: Define the contract
The two things every shipping method must offer.
" Interface with two methods
INTERFACE zif_shipping_method PUBLIC.
METHODS get_name RETURNING VALUE(name) TYPE string.
METHODS get_cost IMPORTING weight_kg TYPE i
RETURNING VALUE(cost) TYPE decfloat34.
ENDINTERFACE.
Part by part (plain text)
- Interface with two methods. An interface has no code, only signatures. Every class that implements it must provide GET_NAME and GET_COST. The caller can rely on both being there.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
For a 10 kg parcel the three methods return their own price through the same call.
What you should see
Standard: 10 Express: 20 Pickup: 0
Try it and check the result
- Activate the interface first, then the three classes, then the demo class. Run the demo with F9: the console shows the three lines.
- Add a fourth class ZCL_SHIP_DRONE that implements the interface and add one NEW line to the list. The loop prints it without any change.
- Remove the method GET_COST from ZCL_SHIP_PICKUP and activate: the syntax check reports that the interface method is not implemented.
Common mistakes
- A class that is FINAL cannot be inherited. Leave the parent open only when you plan an inheriting class.
- Do not inherit just to reuse code. If there is no is-a relationship, use the interface and move the shared code to a helper class.
- Redefining a method and forgetting to call SUPER-> where the parent logic is needed loses that behaviour silently.