OOP ABAP Part 7: Composition vs Inheritance
Preview · a free account unlocks the full lesson
Decide between "is-a" (inheritance) and "has-a" (composition): the fragile base class problem, delegating to helper objects, combining interfaces with composition, and refactoring a deep hierarchy into composed parts.
In this lessonShow sectionsHide sections
Visual overview
Explanation
Part 3 showed inheritance, which is powerful but often overused as a tool for code reuse; this lesson shows the alternative. Inheritance models an "is-a" relationship — a business ticket IS a ticket — while composition models a "has-a" relationship — a booking HAS a pricing strategy.
In composition, an object simply holds references to helper objects and delegates work to them rather than inheriting that work from a superclass.
As a rule, prefer composition for reuse, and reserve inheritance for a genuinely true, stable is-a relationship, since deep hierarchies make any change to a superclass risky for every subclass beneath it — a problem often called the fragile base class.
The trouble with leaning on inheritance for reuse becomes clear with an example: a team builds lcl_order_base, then lcl_order_with_pricing on top of it, then lcl_order_with_pricing_and_tax on top of that, then lcl_export_order on top of that. Every new combination that comes along — export without tax, domestic with special pricing — demands yet another subclass, and a change to lcl_order_base ripples through every one of them. Making matters worse, ABAP only allows a single superclass, so combining two independent capabilities through inheritance alone simply isn't possible.
Create a free account to keep reading
You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.
- Full explanation
- Code example
- Real project scenario
- Common mistakes
- Best practices
- Interview angle
One tap with Google works too. Every topic keeps its first lesson open to everyone.
Keep exploring
Interview questions on this topic
Related SAP topics
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.