Composition over inheritance एक व्यापक रूप से उद्धृत design principle है — objects को base classes से inherit करने (inheritance) के बजाय behaviors को combine करके (composition) बनाने को प्राथमिकता देना। Inheritance के महत्वपूर्ण नुकसान हैं जिन्हें composition टालता है, हालाँकि दोनों की अपनी जगह है।
inheritance बनाम composition
INHERITANCE → a class EXTENDS another, inheriting its behavior ("IS-A" relationship):
class Dog extends Animal
COMPOSITION → a class is BUILT FROM other objects/behaviors ("HAS-A" / uses):
class Car { constructor() { this.engine = new Engine(); } } // composes behaviors
→ composition: combine smaller pieces; inheritance: derive from a parent
