I'm reading J. Bloch's Effective Java and now I'm at inheritance vs composition section. As far as I understood he said that inheritance is not always good.
A related cause of fragility in subclasses is that their superclass can acquire new methods in subsequent releases. Suppose a program depends for its security on the fact that all elements inserted into some collection satisfy some predicate. This can be guaranteed by subclassing the collection and overriding each method capable of adding an element to ensure that the predicate is satisfied before adding the element. This works fine until a new method capable of inserting an element is added to the superclass in a subsequent release.
But why doesn't it work? The superclass is just the Collection
interface and if we add a new method we just a compile-time error. That's not harmful ever...
default
methods to an interface doesn't produce a compile time error either. – Peter Lawrey yesterdayjava.util.Collection
, that is NOT a super class. That is just an interface. – Alderath yesterday