aspose file tools*
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
Author

ListIterator interface

Chris White
Greenhorn

Joined: May 12, 2004
Posts: 2
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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());


[Jess in Action][AskingGoodQuestions]
Chris White
Greenhorn

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

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: http://aspose.com/file-tools
 
subject: ListIterator interface