In the past few posts we have discussed how it can be dangerous to extend a class that we do not control, if it has not been designed for subclassing. We understood this concept with a specific example from Josh Bloch's book: Effective Java. In the example we subclassed the HashSet class in the JDK.
Without repeating the material, I will simply reiterate what I said in my previous post:
The real problem was the class AbstractCollection
which was not designed properly for inheritance. It is a non-final class with a public method addAll(Collection c)
which invokes another non-final public method add(Object o)
.
Here is a possible implementation of the HashSet class that does not break the DRY principle and is also safe for extending...
Comments