Restriction in interface

Z

Zhao

"...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
 
C

Christophe Vanfleteren

Zhao said:
"...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.
 
J

John C. Bollinger

Zhao said:
"...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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top