Limit Class Inheritance Depth (Favor Composition over Inheritance)
Overview
Avoid deep class inheritance hierarchies.
ATC Finding Message:
Avoid deep class inheritance; prefer composition over inheritance
Rationale
Deep class inheritance hierarchies (more than 2-3 levels) create tight coupling, brittle base class syndromes, and hidden dependencies. Subclasses inherit private state and side-effects from ancestors, making changes to base classes unpredictable across the entire codebase.
Clean ABAP Guideline: Favor Object Composition over Class Inheritance. Use interfaces to define contracts and inject dependencies instead of extending deep abstract class hierarchies.
Non-Compliant
" Anti-pattern: 5 levels of concrete inheritance
CLASS zcl_entity DEFINITION ...
CLASS zcl_persisted_entity DEFINITION INHERITING FROM zcl_entity ...
CLASS zcl_sales_document DEFINITION INHERITING FROM zcl_persisted_entity ...
CLASS zcl_order DEFINITION INHERITING FROM zcl_sales_document ...
CLASS zcl_rush_order DEFINITION INHERITING FROM zcl_order ...Compliant
" Clean ABAP: Shallow hierarchy with composition and role-based interfaces
CLASS zcl_rush_order DEFINITION FINAL.
PUBLIC SECTION.
INTERFACES zif_sales_order.
METHODS constructor
IMPORTING
io_persistence TYPE REF TO zif_order_persistence
io_pricing TYPE REF TO zif_pricing_strategy.
ENDCLASS.Configuration & Thresholds
This check can be fine-tuned directly in the ATC Check Variant Editor (CHKV) in Eclipse ADT:
| Parameter Name | Data Type | Default Value | Description & Guidance |
|---|---|---|---|
MAX_DEPTH | Integer | 3 | Maximum allowable inheritance depth levels from standard root object |
Suppression
To suppress findings for this rule, append the pseudo-comment to the statement:
" Standard pseudo-comment:
... "#EC MAX_INHERIT
" SAP CI_ prefix format:
... "#EC CI_MAX_INHERITRelated Rules
- Limit Class Member Attribute Count (Prevent God Classes) —
#EC MAX_ATTRIBUTES - Limit Control Flow Nesting Depth (Guard Clause Enforcement) —
#EC MAX_NESTING - Prefer Composition over Inheritance —
#EC PREF_COMPOSITION
Technical Details
- Native ATC Class:
ZCL_SKYLINT_MAX_INHERIT - Implementation File: zcl_skylint_max_inherit.clas.abap