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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    500
    Jim Cheshire [MSFT]
    Jun 1, 2004
  2. Wildepiet
    Replies:
    0
    Views:
    1,992
    Wildepiet
    Jun 14, 2004
  3. cyberco
    Replies:
    8
    Views:
    657
    cyberco
    Feb 25, 2006
  4. Xah Lee
    Replies:
    15
    Views:
    806
    Sherm Pendley
    Mar 23, 2007
  5. Xah Lee
    Replies:
    15
    Views:
    818
    Sherm Pendley
    Mar 23, 2007
  6. Replies:
    4
    Views:
    465
    timjowers
    Nov 2, 2007
  7. rote
    Replies:
    0
    Views:
    1,330
  8. Xah Lee
    Replies:
    11
    Views:
    688
    Nigel Wade
    Mar 22, 2007
Loading...