views:

47

answers:

1

Item 16 of Effective Java 2nd edition, favor composition over inheritance says the following

"If the superclass acquires a new method in a subsequent release and you have the bad luck to have given the subclass a method with the same signature and a different return type, your subclass will no longer compile.

If you’ve given the subclass a method with the same signature and return type as the new superclass method, then you’re now overriding it"

How likely is that these cases arise in a real world situation? Could anyone here give me an example from a real business app (stripping out proprietary info if needed)?

+3  A: 

This isn't about how likely that scenario is. (Besides, I'm sure you've heard of Murphy's Law)

It's about the fact that composition is much less of a binding contract than inheritance.

Inheritance is a very strong way to bind behavior between classes, and basically Item 16 states that you should use that strong connection only when it is clear that it is necessary. For all other uses, composition should be preferred.

Yuval A