ERPClimb logoERPClimb
OOP ABAP
ABAP Developmentadvanced 11 min read

OOP ABAP Part 7: Composition vs Inheritance

Preview · a free account unlocks the full lesson

Key takeaway

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 sections

Visual overview

OOP ABAP Part 7: Composition vs Inheritance — learning sheetFlow: Run the code example: one order class with two different pricing objects → Add a new tax implementation without changing lcl_order → Write down what the inheritance version would need. Key ideas: THE PROBLEM WITH INHERITANCE FOR REUSE; COMPOSITION AND DELEGATION; BENEFITS; WHEN INHERITANCE IS RIGHT. Watch out: Deep hierarchies make changes to a superclass risky for all subclasses (fragile base class).ERPClimb.com / ABAP / LESSON 7/10 / LEARNING SHEETOOP ABAP Part 7: Composition vs Inheritance1 · HOW IT FLOWS1Run the code exampleone order class with two differentpricing objects2Add a new taximplementation withoutchanging lcl_order3Write down what theinheritance version wouldneed2 · KEY IDEAS EXPLAINEDThe Problem With Inheritance For ReuseA team builds lcl_order_base →lcl_order_with_pricing →lcl_order_with_pricing_and_tax → lcl_export_order…Composition And Delegationmo_pricing TYPE REF TO lif_pricingmo_tax TYPE REF TO lif_taxBenefitsCombine behaviours freely (any pricing with anytax).Replace parts at runtime or in tests (fake taxcalculator).When Inheritance Is RightTrue is-a with shared structure and behaviour,stable over time.Framework requirements: exception classes inheritfrom CX_STATIC_CHECK, some SAP frameworks expectsubclassing of an abstract base (e.g…3 · REMEMBER · WATCH OUTRememberInheritance models "is-a" (a business ticket IS a ticket);composition models "has-a" (a booking HAS a pricing…Composition: an object holds references to helper objects anddelegates work to them.Prefer composition for reuse; use inheritance only for a true, stableis-a relationship.!Watch outDeep hierarchies make changes to asuperclass risky for all subclasses(fragile base class).4 · IN A REAL PROJECTA freight calculation framework had grown to 14 subclasses (road, road-express, road-hazardous, sea,sea-hazardous …). Adding "rail with hazardous surcharge" required copying code from two branchesbecause ABAP has single inheritance. The team refactored into one freight class composed of three strategies…Original ERPClimb learning sheet • Verify against your SAP release and edition

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.

Free account

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

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.