This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces in collections framework  RSS feed

 
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
Bartender
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: 4568
9
  • 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 .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!