• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt Regarding Method Declaration of iterator() in Collection Interface  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Friends,

While going through code of Collection interface, i found that the method
Iterator<E> iterator();
is redeclared in this interface.

As Collection extends Iterable interface, there should not be any need to do so (iterator() method is also declared in Iterable).

I was wondering if there is a special requirement of doing so, and scenarios where we need to implement like this ?

Thanks

 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saurabh kuma wrote:I was wondering if there is a special requirement of doing so, and scenarios where we need to implement like this ?

I suspect it's just to add documentation. If you look at java.util.List you'll see that it also overrides iterator(), basically to tell you that it returns elements in their correct sequence, whereas the java.util.Collection docs say specifically that the order of elements is not guaranteed.

Winston
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I see that question is interesting. I want to add one more question:
In Java swing I met the doc that says that method is overridden because of performance as though it can be quicker found and executed. Something like this but I do not remember very well.
Aren't overridden methods faster than inherited?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:Aren't overridden methods faster than inherited?

Overriding a method does not automatically make it slower or faster - it does not have anything to do with just the fact that it is overridden. The class you were looking at probably has an overridden version of the method that could be written in a more efficient way for that specific class than the method in the superclass.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!