• Post Reply Bookmark Topic Watch Topic
  • New Topic

If ListIterator is an interface, how is it possible to instantiate and call its methods ?  RSS feed

 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, wondering if anyone could answer this question.

According to the API, the method ArrayList.listIterator() "returns a list iterator over the elements in this list (in proper sequence)."

Assume I have an ArrayList called myArrayList (comprised of StringBuilder objects) :



This compiles and runs fine (iterates and prints out the elements in myArrayList).

However, ListIterator is an interface not a class. How is is possible that I can instantiate it (by calling myArrayList.listIterator()) and then use concrete methods .hasNext() and .next() ??

My understanding was that an interface (a) could not be instantiated and (b) could only contain abstract methods ?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different way to look at it:
Think that ListIterator is a name, and you can apply that name to a class. That is actually quite close to the way the Java® Language Specification would talk about it. It says that Iterator is a type, and ListIterator is a subtype of Iterator. As you know, if you have an object of a subtype, you have an IS‑A relationship with the supertype. So you can legitimately say that a ListIterator IS‑An Iterator. And any class which says it implements the Iterator interface IS‑AN Iterator too. So, what if you have a class called MyIterator somewhere and it implements the Iterator interface? You can call its instances MyIterator, or you can call them Iterator objects.

So, you can also call an ArrayList (I presume you mean java.util.ArrayList) a List because ArrayList implements the List interface.

So, any class which implements an interface can be called by the interface name.
List myList = xyz.getList();
Iterator iter = someCollection.iterator();
ListIterator lit = someList.listIterator();
List<XYZ> xyzList = new ArrayList<>();

Somewhere in those calls there is a class which implements the interface named on the left, but only in one instance (4th line) can you actually see what the name of the class is. There is a class hidden away somewhere in xyz or someCollection or someList which implements the interfaces named on the left.
 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ritchie ... so in this line

... the object returned from the method call someArrayList.listIterator() is an instance of some class (which we don't see) that implements the Iterator or ListIterator interface AND that returned object can be allocated to a reference variable of type ListIterator because it shares an IS-A relationship ?



 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. And one more thing: you expect to iterate a List of StringBuilders so the compiler needs to know the List on the right contains StringBuilders only. Which of course you do by declaring it as List<StringBuilder>. Once you have that declaration you may be able to change instances of <StringBuilder> to <>, but only in Java7+.

By the way: why do you want a List of StringBuilders in the first place? You usually use StringBuilders as local variables to alter the content of some bit of text, because you can't alter Strings directly. Once you have finished the alterations you would put the Strings into a List.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!