Restriction in interface

Discussion in 'Java' started by Zhao, Oct 24, 2003.

  1. Zhao

    Zhao Guest

    "...The Set interface extends Collection and contains no methods other
    than those inherited from Collection. It adds the restriction that
    duplicate elements are prohibited..."

    How can Set, an interface, enforce the "no duplicate elements" without
    any implementation?

    My guess is that there is someting in the Collection framework carry
    out this
    enforcement.

    Anyone konws?

    thanks
     
    Zhao, Oct 24, 2003
    #1
    1. Advertising

  2. Zhao wrote:

    > "...The Set interface extends Collection and contains no methods other
    > than those inherited from Collection. It adds the restriction that
    > duplicate elements are prohibited..."
    >
    > How can Set, an interface, enforce the "no duplicate elements" without
    > any implementation?
    >
    > My guess is that there is someting in the Collection framework carry
    > out this
    > enforcement.
    >
    > Anyone konws?
    >


    No, the implementations of Set are supposed to take care of this requirement.
    HashSet for example is backed by a HashMap that is used to enforce the "only
    unique elements" rule.

    --
    Regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Oct 24, 2003
    #2
    1. Advertising

  3. Zhao wrote:

    > "...The Set interface extends Collection and contains no methods other
    > than those inherited from Collection. It adds the restriction that
    > duplicate elements are prohibited..."
    >
    > How can Set, an interface, enforce the "no duplicate elements" without
    > any implementation?


    It cannot. An interface and its documentation specify a contract to
    which implementations are expected to conform. The compiler and VM can
    ensure that method signatures match, but it is up to individual classes
    to implement behavior consistently with the demands of the contract.

    This is really no different from subclassing an existing class. The
    class documentation should define method contracts that specify what the
    methods are supposed to do; subclasses should not violate these
    contracts lest polymorphism be broken, but nothing enforces that. (By
    the way, that is an excellent reason to seperate implementation details
    from general contract details in method documentation.)

    > My guess is that there is someting in the Collection framework carry
    > out this
    > enforcement.


    The concrete classes in the Collections framework all abide by the
    contracts set out in the top-level interfaces' documentation.


    John Bollinger
     
    John C. Bollinger, Oct 24, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RWQ=?=

    file restriction - Forms authentication

    =?Utf-8?B?RWQ=?=, May 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    378
    Jim Cheshire [MSFT]
    Jun 1, 2004
  2. =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=

    Scripted Window Restriction?

    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=, Oct 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    396
    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=
    Oct 5, 2004
  3. z f
    Replies:
    2
    Views:
    5,084
  4. Manglu
    Replies:
    1
    Views:
    986
    Anders =?iso-8859-1?Q?Engstr=F6m?=
    Aug 8, 2003
  5. Arvind
    Replies:
    4
    Views:
    627
    Chris Smith
    Nov 10, 2004
Loading...

Share This Page