Skip to content

Prefer Composition over Inheritance ​

WarningClean ABAP & Modern Syntax#EC PREF_COMPOSITIONRemote Enabled

Overview ​

Prefer composition over inheritance.

ATC Finding Message: Prefer composition over inheritance; only CX_* classes should inherit

Rationale ​

Inheritance creates tight coupling between subclasses and parent implementations, exposes internal state, and complicates refactoring. With the exception of exception hierarchies (CX_* classes) which require inheritance to participate in ABAP error handling, business objects should favor composition by holding references to collaborating classes and delegating tasks.

Non-Compliant ​

abap
CLASS lcl_order_processor DEFINITION INHERITING FROM lcl_order_validator.
ENDCLASS.

Compliant ​

abap
CLASS lcl_order_processor DEFINITION.
  PUBLIC SECTION.
    METHODS constructor IMPORTING io_validator TYPE REF TO lif_order_validator.
ENDCLASS.

Configuration & Thresholds ​

This check can be fine-tuned directly in the ATC Check Variant Editor (CHKV) in Eclipse ADT:

Parameter NameData TypeDefault ValueDescription & Guidance
ALLOW_TEST_CLASSESBooleanfalseAllow class inheritance inside ABAP Unit test includes

Suppression ​

To suppress findings for this rule, append the pseudo-comment to the statement:

abap
" Standard pseudo-comment:
... "#EC PREF_COMPOSITION

" SAP CI_ prefix format:
... "#EC CI_PREF_COMPOSITION

Technical Details ​

Released under the MIT License.