Composition over Inheritance
Last updated
What is Composition over Inheritance?
Composition over inheritance is a programming principle that suggests that it is better to use composition, a mechanism for assembling objects, to create complex objects, rather than using inheritance, which is a mechanism for creating new classes based on existing ones.
Inheritance is a powerful mechanism for creating reusable code, but it can also lead to tightly coupled, hard-to-maintain code. This is because inherited classes are tightly bound to their parent classes and any changes made to the parent class will affect all of its child classes. This makes it hard to change or extend the code without affecting the entire class hierarchy.
Free resources to learn Composition over Inheritance
More in stage 7: Messaging
Composition over Inheritance: common questions
Can I learn Composition over Inheritance in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Composition over Inheritance in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this Composition over Inheritance tutorial for beginners?
It is lesson 49 of 94 in the free Software Design and Architecture roadmap, in stage 7, Messaging. If you are new to Software Design and Architecture, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.