Composition over inheritance ਇੱਕ ਵਿਆਪਕ ਤੌਰ 'ਤੇ ਹਵਾਲਾ ਦਿੱਤਾ ਜਾਣ ਵਾਲਾ ਡਿਜ਼ਾਇਨ ਸਿਧਾਂਤ ਹੈ — objects ਨੂੰ behaviors ਨੂੰ ਜੋੜ ਕੇ (composition) ਬਣਾਉਣ ਨੂੰ ਤਰਜੀਹ ਦਿੰਦੇ ਹੋਏ base classes ਤੋਂ ਵਿਰਾਸਤ (inheritance) ਲੈਣ ਦੀ ਬਜਾਏ। 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
