Abidi
Abidi

Reputation: 8006

Java Set Stipulation

Java docs says the following about Set interface, can someone please help me understand how these additional stipulations are forced? I mean you cannot throw an extra exception that is not thrown by super interface (in this case Collection).

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.

Thanks

-Abidi

Upvotes: 0

Views: 182

Answers (2)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74800

The stipulations are not forced, they are just added to the contract.

Luckily the contracts of the methods in Collection (and Object) are loose enough that an implementation can implement both the contract from Collection, Object and Set without violating any of these.

For example, the add method does not throw Exceptions when trying to add an object which is already in it, it just returns false (instead of true). This is already allowed by Collection.add:

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

Upvotes: 1

SLaks
SLaks

Reputation: 887867

These are contractual stipulations.

It means that all Sets are expected to obey these rules.
They aren't enforced.

Upvotes: 3

Related Questions