🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Programming Concepts Intermediate

What is Composition over Inheritance?

A design principle favoring object composition (has-a relationships) over class inheritance (is-a relationships) for code reuse.

Composition over inheritance advises building complex behavior by combining simple, focused objects rather than creating deep inheritance hierarchies. Inheritance creates tight coupling between parent and child classes, and deep hierarchies become fragile — changing a base class can break all descendants. Composition is more flexible: behaviors can be mixed and matched at runtime, components can be replaced independently, and testing is simpler (mock individual components). In PHP, traits provide a limited form of composition within the class system. The Strategy pattern exemplifies composition — injecting different algorithm implementations rather than subclassing for each variation.

Related Terms

Technical Debt
The implied cost of additional rework caused by choosing a quick solution now instead of a better approach that would take longer.
Factory Pattern
A creational design pattern that provides an interface for creating objects without specifying their exact classes.
Thread
The smallest unit of execution within a process, allowing concurrent operations to run within a single program.
Event-Driven Architecture
A software design pattern where components communicate by producing and consuming events rather than direct method calls.
Race Condition
A bug that occurs when the behavior of software depends on the timing or order of uncontrolled events like thread scheduling.
Debugging
The process of finding and fixing errors (bugs) in software code to ensure correct program behavior.
View All Programming Concepts Terms →