2

For example in Collection interface, we have: From Java Documentation - Collection Interface

If the method is optional it means (I assume) that it does not have to be implemented. But, abstract methods HAVE to be implemented by concrete classes. What am I missing? Do they mean we can have empty methods?

MilaHalina
  • 138
  • 6
  • Code-wise you have to implement it. But nothing stops you from implementing it with meaningless code. For example just throwing an exception if it is called. So while there is no language support for this "optional", you can still do it that way. – Zabuzard Dec 09 '20 at 17:14

1 Answers1

1

From https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#clear--

void clear()

Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns.

Throws: UnsupportedOperationException - if the clear operation is not supported by this collection

It just means that it has to be implemented, but you can state in the docs that you do not support it (for whatever reason), and then you should just throw an UnsupportedOperationException.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103