Adobe Interview Question
Software Engineer / DevelopersAlways tries to use the composition over inheritance if possible to make the design more flexible and to avoid coupling. Composition means to use the solution of various sub-problems to solve the big problem. Because in case of composition you use the object of the super class as a data member of a class, which is responsible for doing some unique job. If any change require to be done in any super-class then rest of the sub-modules or classes are not required to be changed while this is not true in case of inheritance. So, composition provides more flexibility over inheritance. Inheritance provides "is a" relationship and composition provides "has a" relationship.
My answer would be depends. Composition is extension at runtime, while Inheritance is extension at design time.
- Amit November 03, 2009