i just found while studying the JDK 1.6 ArrayBlockingQueue – The constructor makes a call to one of the public overridable methods ! I thought this is a bad practice specially for an API.
public ArrayBlockingQueue(int capacity, boolean fair,
Collection<? extends E> c) {
this(capacity, fair);
if (capacity < c.size())
throw new IllegalArgumentException();
for (Iterator<? extends E> it = c.iterator(); it.hasNext();)
add(it.next()); // -> surprise: add is public
}
I was actually trying to extend the ArrayBlockingQueue , and add some some state and overrided add() , and I promptly got the java.lang.NullPointerException when I invoked super(capacity,fair,col) constructor. Am I missing some design concept here ?
This isn’t so out of the ordinary; essentially, you can think of this as just a one-line syntax for the lines
which is overridable. Calling overridable methods isn’t usually a good idea, but it’s perfectly reasonable here.
But…that said, extending any collection from
java.utilorjava.util.concurrentis kind of evil. You should decorate them, rather than extending them directly.