But Java language requires that every method in an interface is implemented by every implementation of that interface. There are no exceptions to this rule. It confused me for this point and I did some research. It turned out that the implementation is not really optional but it can be empty.
Take the remove() method of interface Iterator<E> as en example. The signature is
void remove();
To make this implementation optional, one can do one of following two. Either way, you are implementing the method.
void remove() { // empty }
void remove() { throw new UnsupportedOperationException(); }This is the real meaning of "optional" implementation. Of course, one can choose to actually implement the method like below.
void remove() { // do some thing here // validate state and remove element }