Consider composition instead of subclassing
by, 04-26-2012 at 06:16 PM (969 Views)
Composition might be considered as subclassing’s alternative.
- is simply implemented by all call forwarding to object field.
- consists of zero dependence upon the object field’s implementation details.
- is considerably much more flexible, as it is dynamically defined at the runtime instead of compile time
Subclassing issues are :
- Encapsulation is violated since the implementation of subclass & superclass gets tightly coupled.
- Addition of the new method to superclass might happen to break the subclass.
- Evolvation of the subclass & superclass is needed, in paralled.
- A class designing so that extension may do extra work.
- Different programmers control different packages which means that class extension is risky in a different package.