Optional Methods in Java Interface

后端 未结 12 1867
说谎
说谎 2020-11-30 20:53

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.

12条回答
  •  被撕碎了的回忆
    2020-11-30 21:40

    Oracle's Java Collections Tutorial:

    To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. (Such variants might include immutable, fixed-size, and append-only.) Instead, the modification operations in each interface are designated optional — a given implementation may elect not to support all operations. If an unsupported operation is invoked, a collection throws an UnsupportedOperationException. Implementations are responsible for documenting which of the optional operations they support. All of the Java platform's general-purpose implementations support all of the optional operations.

提交回复
热议问题