🎁 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

Thread
The smallest unit of execution within a process, allowing concurrent operations to run within a single program.
API Design
The practice of designing application programming interfaces that are consistent, intuitive, and maintainable for developers to consume.
Refactoring
Restructuring existing code without changing its external behavior to improve readability, maintainability, and performance.
Immutable Object
An object whose state cannot be modified after creation, providing thread safety and predictable behavior in concurrent systems.
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 →