This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Interfaces in collections framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces in collections framework" Watch "Interfaces in collections framework" New topic
Author

Interfaces in collections framework

Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

My question is

Interface Set has method add(E e) and it extends interface Collection.
Interface Collection also has method add(E e)
So why do we need the same method in interface Set , since it already extends interface Collection .
What is the purpose ?
I am stuck with that


Understanding is Everything - Peter Lord
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If you compare the javadoc of those interfaces you'll see a differences. The Set interface defines a more specified contract.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Wouter Oet wrote:If you compare the javadoc of those interfaces you'll see a differences. The Set interface defines a more specified contract.


Yes I understand that but it is an interface , so does it matter that it tells about no duplicate elements can be added and so on.
It does not implement anything .
So what is the purpose.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

The purpose is to tell developers what the method should do.

If you implement your own Set, it can't make you implement it so that it doesn't allow duplicate elements. All it can do is tell you what the method should do. If you don't follow the contract it specifies, you're going to cause problems.
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Thank you matthew and Wouter for the help I got it .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interfaces in collections framework
 
Similar Threads
doubt in priority queue in java 1.5
Generics
Collection
from mock test
Why Map doesn't extend collection interface while List and Set can extend Collection interface