16 April 2008

Inherently Undesirable

by mo

I’m currently reading:

Effective Java(TM) Programming Language Guide (The Java Series)
by Joshua Bloch Read more about this book...

I would like to share en excerpt that I read this morning on the bus… it’s about my favorite topic.

Implementation Inheritance

Inheritance is a powerful way to achieve code reuse, but it is not always the best tool for the job. Used inappropriately, it leads to fragile software. It is safe to use inheritance within a package, where the subclass and the superclass implementation are under the control of the same programmers. It is also safe to use inheritance when extending classes specifically designed and documented for extension. Inheriting from ordinary concrete classes across package boundaries, however, is dangerous. As a reminder, this book uses the word ‘inheritance’ to mean ‘implementation inheritance’ (when one class extends another). The problems discussed in this item do not apply to interface inheritance (when a class implements an interface or where one interface extends another).

"Unlike method invocation, inheritance breaks encapsulation. In other words, a subclass depends on the implementation details of its superclass for its proper function.