View RSS Feed

Java Object

Consider composition instead of subclassing

Rate this Entry
by , 04-26-2012 at 06:16 PM (969 Views)
Composition might be considered as subclassing’s alternative.

Composition :
  • 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.

Submit "Consider composition instead of subclassing" to Facebook Submit "Consider composition instead of subclassing" to Digg Submit "Consider composition instead of subclassing" to del.icio.us Submit "Consider composition instead of subclassing" to StumbleUpon Submit "Consider composition instead of subclassing" to Google

Categories
Composition

Comments