Composition offers flexibility; inheritance provides a clearer hierarchy.
Composition involves building classes using other classes, offering more flexible and reusable code. Inheritance creates a clear 'is-a' relationship. In an e-commerce system, a 'ShoppingCart' could use composition to include multiple 'Product' objects, allowing easy swapping and reusability without tight coupling.
Additional Notes
What are trade-offs in using composition over inheritance?