File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes ListIterator interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ListIterator interface" Watch "ListIterator interface" New topic

ListIterator interface

Chris White

Joined: May 12, 2004
Posts: 2
I'm having a real headache trying to understand why this is so, I probably
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

Joined: Jul 08, 2003
Posts: 24199

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

[Jess in Action][AskingGoodQuestions]
Chris White

Joined: May 12, 2004
Posts: 2
Thanks so much! Headache has now gone!
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: ListIterator interface
It's not a secret anymore!