• Post Reply Bookmark Topic Watch Topic
  • New Topic

about methods in interface  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java.Util package , there is an interface called Iterator..

the hasNext() and some more methods in Iterator interface are supposed not to be defined in interface..
they must be defined by concrete class which implements it...

how is it defined in interface?
 
Arafat Nalkhande
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods (hasNext() , next() and remove() ) are not defined in the interface. They are merely declared there.

Its implemented by other collection classes which have methods with return type as Iterator.
For example the AbstractList implements it and returns the Iteraror objec tin repsonse to iterator() method call.
 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the class (LinkedHashSet), which implements interface iterator,there is no defined hasNext() method ..

how does the interator reference, which is assigned by ob.iterator(), get the hasNext() method in ipmlemented class?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It implements the Iterable interface - not the Iterator interface.
All interfaces should be abstracted back to a type that contains all referentially transparent operations. This is often referred to as "immutable". Iterable is "immutable", Iterable is not; this allows Iterable to be used in the for each loop.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedHashSet does not implement Iterator. The object that gets returned by LinkedHashSet.iterator() implements Iterator, but that's not a LinkedHashSet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!