Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to understand method belongs to which interface and who define its body in Collection Framework?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been reading Collection Framework but I'm quite baffled about these interface methods. Collection is the root of all those sub interfaces example List, Set etc.
Collection interface(java.util) extends Iterable interface which is in java.lang.
Here Collection interface has a method boolean add(E e): Ensures that this collection contains the specified element (optional operation) Ref: Java 8 API Doc.

Same method is in List interface, I got this List extends Collection so might have overridden then ArrayList implements this List interface. As far as I know we can't define method in an interface (Except Java 8 default and static methods) then who does define body of this method which has code to add an element in the ArrayList ? does ArrayList defines its implementation according to its features like duplicates allowed etc. (& same method with LinkedList) ?

When I click on boolean add(E e) method in Method Summary of ArrayList Doc Page. It is written there like this
Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E> what does this means ? does this mean these two interfaces just declares methods ?

Where this method is defined and If someone ask whose method is this boolean add(E e) what should I answer ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7797
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection and List both declare the add(T) method. List.add(T) overrides Collection.add(T) to further specify the behavior: Not only must the List guarantee it contains the element after it's been added, it must guarantee that it's been added to the end of the list. The behavior may not conflict with the behavior specified by the Collection interface.

The definition is up to the implementing class: ArrayList and LinkedList both have their own implementations, but both adhere to the contract specified by List, and so, the contract specified by Collection.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:List.add(T) overrides Collection.add(T) to further specify the behavior
further specify the behavior means from programming perspective List just declares (If List want to have different behavor than method specified in Collection) add(T) method with different signature is this right ?
Stephan van Hulst wrote:Not only must the List guarantee it contains the element after it's been added, it must guarantee that it's been added to the end of the list.
How an interface(Here List) can guarantee It appends elements where an interface can only have specified or declared methods only, please correct me If I'm wrong.

When you say the constraints or specific rules of a particular interface for an example List which allows duplicate elements, do you indirectly mean the classes which implements this List interface as we know ArrayList and Vector class implements List interface and both have their own distinct implementation(defined method having code which guarantee the specifications in these classes ? ).

Example ArrayList is not synchronized where as Vector is. We can't have duplicates in HashSet(Set interface) but we can in ArrayList. please guide me, once I'm clear why all these interfaces, who does what then I feel content so can go further else just mugging up their methods won't make sense.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got it,
  • Collection interface has add(T) method
  • AbstractCollection an abstract class implements Collection interface and overrides Collection.add(T) method, also defines it
  • List interface implements Collection interface and overrides Collection.add(T) method
  • AbstractList an abstract class extends AbstractCollection and implements List interface and overrides List.add(T) method, also defines it
  • ArrayList a class extends AbstractList implements List interface and overrides List.add(T) method and also define it

  • I've drawn a diagram of this please have look at it ArrayList Hierarchy
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:further specify the behavior means from programming perspective List just declares (If List want to have different behavor than method specified in Collection) add(T) method with different signature is this right ?

    The main objective of inheritance is type specialization i.e. a subtype is a specialized form of its supertype. In this case, the Collection interface’s boolean add(E e) method is overridden in the List interface with a specification that is more specialized.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joe Bishara wrote:In this case, the Collection interface’s boolean add(E e) method is overridden in the List interface with a specification that is more specialized.
    yes I know that, what I wanted to know is where the code of that method is written as we can't have defined method in interfaces though it is Collection or List. But I think I got the answer as I mentioned above & also attached a diagram but was waiting for the confirmation from someone who are seniors here.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7797
    142
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:How an interface(Here List) can guarantee It appends elements where an interface can only have specified or declared methods only, please correct me If I'm wrong.

    An interface can't guarantee anything. It just specifies behavior. When I say "a List must guarantee", I mean that anything that implements the interface List must guarantee what's specified by the contract, otherwise it's an error of the implementer.

    When you say the constraints or specific rules of a particular interface for an example List which allows duplicate elements, do you indirectly mean the classes which implements this List interface as we know ArrayList and Vector class implements List interface and both have their own distinct implementation(defined method having code which guarantee the specifications in these classes ? ).

    Yes. The thing is, most of the time we don't care if something is a Vector or an ArrayList. When the methods and classes I write require a collection to be ordered, they accept List, because anything that implements List guarantees that the elements are ordered, no matter if it's a Vector or an ArrayList.

    Example ArrayList is not synchronized where as Vector is. We can't have duplicates in HashSet(Set interface) but we can in ArrayList. please guide me, once I'm clear why all these interfaces, who does what then I feel content so can go further else just mugging up their methods won't make sense.

    Sets require that each element is unique. When I require a collection containing only unique elements, my methods accept a Set. I don't care whether it's a HashSet or a TreeSet, only that the elements are unique. If I want the elements to be both unique and sorted, I use a SortedSet.

    To make your programs flexible, you should always accept the least specific interface that your methods require.

    But I think I got the answer as I mentioned above & also attached a diagram but was waiting for the confirmation from someone who are seniors here.

    AbstractCollection and AbstractList provide default implementations for most methods of their respective interfaces. As a matter of fact, the add() method by default throws UnsupportedOperationException, which is allowed because the Collection and List interfaces stipulate that the add() method is optional. It's up to the concrete class (such as ArrayList or Vector) to override add() so that it doesn't throw that exception.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That was the information I was looking for, mostly about AbstractCollection, AbstractList and optional methods of interfaces. I was stuck since I posted because just reading without understanding makes me feel uncomfortable. Now much much satisfied. Thank you so much
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7797
    142
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm glad it helped
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!