Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces in collections framework

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you compare the javadoc of those interfaces you'll see a differences. The Set interface defines a more specified contract.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you matthew and Wouter for the help I got it .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic