From my understanding if you implement an interface in java, the methods specified in that interface have to be used by the sub classes implementing the said interface.
I’ve noticed that in some interfaces such as the Collection interface there are methods which are commented as optional, but what exactly does this mean? Its thrown me a bit as I thought all methods specified in the interface would be required?
There seems to be an awful lot of confusion in the answers here.
The Java language requires that every method in an interface is implemented by every implementation of that interface. Period. There are no exceptions to this rule. To say “Collections are an exception” suggests a very fuzzy understanding of what’s really going on here.
It’s important to realize that there are sort of two levels of conforming to an interface:
What the Java language can check. This pretty much just boils down to: is there some implementation for each of the methods?
Actually fulfilling the contract. That is, does the implementation do what the documentation in the interface says it should?
Well written interfaces will include documentation explaining exactly what is expected from implementations. Your compiler can’t check this for you. You need to read the docs, and do what they say. If you don’t do what the contract says then you’ll have an implementation of the interface as far as the compiler is concerned, but it will be a defective/invalid implementation.
When designing the Collections API Joshua Bloch decided that instead of having very fine-grained interfaces to distinguish between different variants of collections (eg: readable, writable, random-access, etc.) he’d only have very coarse set of interfaces, primarily
Collection,List,SetandMap, and then document certain operations as “optional”. This was to avoid the combinatorial explosion that would result from fine-grained interfaces. From the Java Collections API Design FAQ:When methods in the Collections API are documented as being “optional operations”, it does not mean that you can just leave the method implementation out in the implementation, nor does it mean you can use an empty method body (for one thing, many of them need to return a result). Rather, it means that a valid implementation choice (one that still conforms to the contract) is to throw an
UnsupportedOperationException.Note that because
UnsupportedOperationExceptionis aRuntimeExceptionyou can throw it from any method implementation, as far as the compiler is concerned. For example, you could throw it from an implementation ofCollection.size(). However, such an implementation would violate the contract as the documentation forCollection.size()does not say that this is permitted.Aside: The approach used by Java’s Collections API is somewhat controversial (probably less now than when it was first introduced, however). In a perfect world, interfaces would not have optional operations, and fine grained interfaces would instead be used. The problem is that Java supports neither inferred structural types or intersection types, which is why attempting to do things the “right way” ends up becoming extremely unwieldy in the case of collections.