Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions _rules/0110.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
rule_id: 0110
rule_category: general
title: Prefer composition over class inheritance
Comment thread
dennisdoomen marked this conversation as resolved.
Outdated
severity: 2
---
Inheritance creates a tight coupling between a base class and its derived classes. Any change to the base class can unexpectedly break derived classes, and the inheritance hierarchy can become difficult to understand as it grows. Composition — building behavior by combining small, focused objects or interfaces — avoids these problems.

Prefer composition when:
- The relationship is "has-a" rather than "is-a".
- You want to reuse behavior without exposing or depending on the internals of another class.
- You need to vary behavior at runtime (e.g. through the [Strategy pattern](https://en.wikipedia.org/wiki/Strategy_pattern)).

**Exception:** Inheritance is appropriate when a true "is-a" relationship exists and when derived classes genuinely extend (rather than replace) the behavior of their base class. Always follow the [Liskov Substitution Principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) when using inheritance.