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 3240002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:02:08+00:00 2026-05-17T18:02:08+00:00

I just looked up the Set interface and found that it mostly (or completely)

  • 0

I just looked up the Set interface and found that it mostly (or completely) only redeclares functions which are already in the Collection interface. Set itself extends Collection, so doesn’t that mean that the Set interface automatically has all the functions from Collection? So why are they redeclared then?

For example, Set redeclares this:

/**
 * Returns the number of elements in this set (its cardinality).  If this
 * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 * <tt>Integer.MAX_VALUE</tt>.
 *
 * @return the number of elements in this set (its cardinality)
 */
int size();

/**
 * Returns <tt>true</tt> if this set contains no elements.
 *
 * @return <tt>true</tt> if this set contains no elements
 */
boolean isEmpty();

And the declaration in Collection:

/**
 * Returns the number of elements in this collection.  If this collection
 * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 * <tt>Integer.MAX_VALUE</tt>.
 *
 * @return the number of elements in this collection
 */
int size();

/**
 * Returns <tt>true</tt> if this collection contains no elements.
 *
 * @return <tt>true</tt> if this collection contains no elements
 */
boolean isEmpty();

This seems very redundant to me. Why not just define the Set interface as:

public interface Set<E> extends Collection<E> {}

I think there is no single difference between those interfaces, right?


Of course I am not asking about the different semantics / meaning of Set. I know that. I am just asking about if it technically (i.e. to the compiler) has any difference. I.e., speaking generally:

interface A { void foo(); }
interface B extends A { void foo(); }
interface C extends A {}

Now, is there any difference between A, B or C?


While the contract (i.e. what is said in the documentation) can really be different for some functions (as for add), there is a valid reason to redeclare them: To be able to put a new documentation, i.e. to define the new contract.

However, there are also functions (like isEmpty) which have exactly the same documentation / contract. Why are they also redeclared?

  • 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-05-17T18:02:09+00:00Added an answer on May 17, 2026 at 6:02 pm

    Technically for the compiler it makes no difference at all.

    However, a set cannot have duplicate entries whereas a Collection can. This is worth knowing about.

    Because of this, the methods semantics for parameters, return values and what happens can mean different things. Redeclaring also allows the javadoc to become more specific. For example for add():

    Set: @return true if this set did not already contain the specified element

    Collection: @return true if this collection changed as a result of the call

    The meaning for set is more specific.

    Even for methods that are not more specific, it enables the javadoc to be nicer. For example, for size() : “Returns the number of elements in this set (its cardinality).” which is closer to the language people used to mathematical sets will understand.

    The API documents summarise this by saying: “The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.)”

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

Sidebar

Related Questions

Just looking for the first step basic solution here that keeps the honest people
just a quick question: I am a CS undergrad and have only had experience
Just how much slower are events? I have written a streaming XML parser (that
Just found this out, so i am answering my own question :) Use a
I'm looking for a portable interface to POSIX alarm(2) (or similar) in Ruby. That's
I'm wanting to use an extension for Python that I found here , but
I'm building a set of SVG files that include an unfortunate number of hardcoded
Just what the title says, I need to change the password for an existing
Just bought a 2.4GHz Intel Core 2 Duo iMac with 2GB of memory and
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.

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.