This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ListIterator interface

 
Chris White
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a real headache trying to understand why this is so, I probably
Hello!
I am getting totally confused but...
After creating an ArrayList object and populating it with values using the add(Object o) method. I then iterate around the ArrayList by obtaining a ListIterator using the listIterator() method.
Now ListIterator is an Interface according to the API, so how come you can call it's methods such as hasNext()?? Surely an interface has all abstract methods and therefore needs to be implemented by a concrete class??
Please help, I have a headache trying to figure this out.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
Welcome to JavaRanch!
The listIterator() method returns an instance of a class that implements the ListIterator interface. A class that implements an interface can be passed as an argument of the interface type, or returned from a method as that interface, or assigned to a variable of that interface type.
If you're curious as to the name of that class, try something like
System.out.println(myArrayList.listIterator().getClass().getName());
 
Chris White
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much! Headache has now gone!
Chris
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object-oriented principle behind your old headache is called Polymorphism. It's a topic worth learning well.

To further develop a good understanding of Polymorphism, I recommend taking a look at the "How my Dog learned Polymorphism" story of the JavaRanch Campfire Stories.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic