• Post Reply Bookmark Topic Watch Topic
  • New Topic

why iterator implementation is dont in AbstractArray class  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

can any one tell me why java has provided the implementation for Iterator as an inner class inside the AbstractArrayList class?

why it needs to go for inner class?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, there is no class AbstractArrayList in the Java API. I assume you mean some other class in the collections API.

It doesn't need to be an inner class. The programmer who wrote the implementation of the class just chose to do it that way.

Sometimes there are different ways of programming something something and it's up to the programmer to choose. If you really want to know why, you'd have to ask the programmer, and maybe he or she doesn't even have a clear answer to it. Maybe he or she just chose the first thing that popped up into his or her mind.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Itr class (and ListItr class)are only used within an AbstractList - other classes may use instances of Itr or ListItr, but they only need to know about the interfaces, Iterator and List Iterator. Using a private inner class here is a convenient way to limit the declaration of Itr and ListItr so they are only visible from within AbstractList. It's generally conisdered good programming practice to limit scope and visibility to the minimum required.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!