Question

What does "optional" mean in this context?

public interface Collection<E> extends Iterable<E> 
{
 // Basic operations
   int size();
   boolean isEmpty();
   boolean contains(Object element);
 // optional

The last line comment "//optional": what does it mean? I tried to implement a Collection by implementing it public class Col-implementation implements Collection and it's not optional at all.

I need to implement all the methods, even the ones commented as "optional". I am clearly not getting what they mean by "optional". Any insight about that?

Was it helpful?

Solution

You must provide an implementation, but that implementation can throw an UnsupportedOperationException.

OTHER TIPS

In this context optional means that not all Collection<E> may do something useful in the implementation. Although providing an implementation is indeed mandatory, it is OK to throw an exception to indicate that the method is not implemented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top