Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:15:21+00:00 2026-06-03T20:15:21+00:00

From my understanding if you implement an interface in java, the methods specified in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:15:23+00:00Added an answer on June 3, 2026 at 8:15 pm

    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:

    1. What the Java language can check. This pretty much just boils down to: is there some implementation for each of the methods?

    2. 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, Set and Map, 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:

    To illustrate the problem in gory detail, suppose you want to add the
    notion of modifiability to the Hierarchy. You need four new
    interfaces: ModifiableCollection, ModifiableSet, ModifiableList, and
    ModifiableMap. What was previously a simple hierarchy is now a messy
    heterarchy. Also, you need a new Iterator interface for use with
    unmodifiable Collections, that does not contain the remove operation.
    Now can you do away with UnsupportedOperationException? Unfortunately
    not.

    Consider arrays. They implement most of the List operations, but not
    remove and add. They are “fixed-size” Lists. If you want to capture
    this notion in the hierarchy, you have to add two new interfaces:
    VariableSizeList and VariableSizeMap. You don’t have to add
    VariableSizeCollection and VariableSizeSet, because they’d be
    identical to ModifiableCollection and ModifiableSet, but you might
    choose to add them anyway for consistency’s sake. Also, you need a new
    variety of ListIterator that doesn’t support the add and remove
    operations, to go along with unmodifiable List. Now we’re up to ten or
    twelve interfaces, plus two new Iterator interfaces, instead of our
    original four. Are we done? No.

    Consider logs (such as error logs, audit logs and journals for
    recoverable data objects). They are natural append-only sequences,
    that support all of the List operations except for remove and set
    (replace). They require a new core interface, and a new iterator.

    And what about immutable Collections, as opposed to unmodifiable ones?
    (i.e., Collections that cannot be changed by the client AND will never
    change for any other reason). Many argue that this is the most
    important distinction of all, because it allows multiple threads to
    access a collection concurrently without the need for synchronization.
    Adding this support to the type hierarchy requires four more
    interfaces.

    Now we’re up to twenty or so interfaces and five iterators, and it’s
    almost certain that there are still collections arising in practice
    that don’t fit cleanly into any of the interfaces. For example, the
    collection-views returned by Map are natural delete-only collections.
    Also, there are collections that will reject certain elements on the
    basis of their value, so we still haven’t done away with runtime
    exceptions.

    When all was said and done, we felt that it was a sound engineering
    compromise to sidestep the whole issue by providing a very small set
    of core interfaces that can throw a runtime exception.

    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 UnsupportedOperationException is a RuntimeException you can throw it from any method implementation, as far as the compiler is concerned. For example, you could throw it from an implementation of Collection.size(). However, such an implementation would violate the contract as the documentation for Collection.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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From what little understanding of Cassandra I have, it seems that data locality is
Background: I have three Silverlight Pages, that implement my interface: interface IPageWithData<in T> where
From my understanding, each of these methods: get() and put() are atomic. But, when
I have some not understanding actions from gnu clisp Suppose, I have some code
My understanding is that it's advised testers are separate from developers, i.e you obviously
It is my understanding that although Mercurial has support from branches, the community generally
It's my understanding that if two threads are reading from the same piece of
In my quest to correctly grasp Interface best practices, I have noticed declarations such
So far, I almost always worked with no-interface EJBs and have a slight understanding
I have specified a couple of interfaces, which I am implementing as entities using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.