• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Methods that return interfaces

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that this isn't an Intermediate-level question, but I was working with a LinkedList, specifically the listIterator() method which returns a ListIterator object.
When I check the API docs, I find that the ListIterator is an interface which inherits from the Iterator interface.
Where is the class used to create the ListIterator object returned from the listIterator() method? Or in other words, when you use any of the methods of the ListIterator interface, what class holds the implementations for those methods, if any?
In the hierarchy, it ends at the ListIterator interface. I'm sure that this isn't a single instance of this occurrence, so I guess I'm questioning any case where this happens.
Thanks in advance!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,
Only the individual implementation of listIterator() knows the answer to your question. Each implementation of listIterator() returns a different implementation of this interface, specifically written to iterate over a given kind of list (obviously, the most efficient way to write a ListIterator for an ArrayList, based on an array, is going to be different than how you'd write one for a LinkedList!)
Many iterators are implemented as inner classes of the Collection class that defines them; this gives the iterator class access to the Collection's private data, so that the Iterator can be written more cleanly.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As mentioned, the class may be implemented as an inner class, which is most likely also private. This would explain why it doesn't appear in the API docs, which only document public and protected members.
Typically, it doesn't really matter what the exact implementation is, just as long as it obeys the contract of the specified interface.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun does a great service in providing source code for most of the base Java classes, which means you can look directly into the code to see what's going in. In this specific case, the source for "java.util.LinkedList" includes the following, and I hope I'm not violating some license agreement by posting this here ;-)

The class ListIter is in nested (inner) class that implements all of the behavior of a "ListIterator". It is private so you can't willy-nilly create one, but when you call the "listIterator(index)" method a "ListIter" is instantiated and returned to you. Since its methods are public you can call them.
Ernest has explained the advantages of doing it this way. It's a nice model to understand.
 
Tim Oister
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for the answers to this question, and big thanks to Wayne for the code snippet. As soon as I saw the mention of inner classes, the light came on. Makes perfect sense now!
Thanks again!
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic