0

So it's only been few days that I touched on collection classes and stumbled upon a thing called "Iterator". And after few prying and poking I again stumbled upon docs.oracle.org and there I learned that Iterator is actually an interface and still we create it's object.

Iterator itr = myPrecious.iterator();

Is the "itr" not an object?? or am I missing something?? Wasn't it impossible to make object of an interface??

and what is that special thing

myPrecious.iterator(); ??

wasn't it

new Iterator(); to instantiate an object??

Edit : forgot to mention Javas is kinda my first programming language so forgive my stupidity.

  • 1
    This is very basic java. A class can implement an interface, and then that class can be instantiated! I suggest you pick up a good book about Java and read the whole chapter in interfaces. – GhostCat Oct 18 '20 at 15:47
  • 1
    If a class implements an interface, you can create instances of that class and refer to them by the interface: `MyInterface instance = new ClassThatImplementsMyInterface();`. Also, methods have return types, so you can instantiate it by using a method that returns an object of the appropriate type too: `MyInterface instance = anotherObject.methodThatReturnsSomethingCompatibleWithMyInterface();` – JustAnotherDeveloper Oct 18 '20 at 15:47
  • 1
    Think of interfaces as a “looks like a” specification, and classes as a “is a” specification. Consider also, if you *couldn't* create an object that implemented an interface, then there would be no use for interfaces, so they wouldn’t exist. But they do exist, so you must be able to instantiate objects that implement interfaces. Relevant: [Liskov Substitution Principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) – Bohemian Oct 18 '20 at 15:57
  • Related: [Is it possible to create an object of an interface in java?](https://stackoverflow.com/questions/10172823/is-it-possible-to-create-an-object-of-an-interface-in-java) – Ole V.V. Oct 18 '20 at 16:50

2 Answers2

2

Is the "itr" not an object??

It's a reference.

Wasn't it impossible to make object of an interface??

You can not instantiate an interface. Here, a parent type (Iterator) reference is referencing an object of child type.

and what is that special thing

myPrecious.iterator(); ??

Here iterator is a function in the class whose object is myPrecious. Check the definition of the function, iterator here for an example.

wasn't it

new Iterator(); to instantiate an object??

You can instantiate a non-abstract class using the keyword, new. You can instantiate an anonymous class by using new on the interface name as shown here for an example.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

The point of an interface is:

  1. You cannot directly create an instance of the interface, an object, you are correct.
  2. A class may implement the interface. It declares that it implements the interface and it contains all the methods that the interface contains (unless it’s an abstract class, but then again you can’t create objects from it, so let’s forget this situation for now).
  3. You may assign a reference to an instance (object) to a variable (or parameter) that is declared to have the interface type.

So to answer your questions:

Iterator itr = myPrecious.iterator();

Is the "itr" not an object?? …

myPrecious.iterator() returns a real object, and a reference to the object is stored into itr. The object probably belongs to some class that we haven’t heard of and do not need to care about. All that we know is that that class implements the Iterator interface. This means that we can use the iterator as specified by that interface.

wasn't it

new Iterator(); to instantiate an object??

Good question. Answer: In the end it is. However, very often in real-world programming we are calling an ordinary method in order to get a new object. That method must in turn use new. Or call another method that uses new (etc.). And this is where things go nicely hand in hand: since we don’t know the actual class of the object, we cannot use new ourselves. But myPrecious, your collection object, does know the class to use for instantiating an iterator object, so it can use new for us. And return the created iterator object to us.

One way to check is through this little code experiment:

    List<String> myList = new ArrayList<>();
    Iterator itr = myList.iterator();
    System.out.println(itr.getClass());

On my Java 11 it prints:

class java.util.ArrayList$Itr

Output on other java versions may be different. You notice that the output doesn’t mention the Iterator interface as the class of the iterator object, but instead some ArrayList$Itr. This means a class named Itr declared inside the ArrayList class. So yes, the iterator is really an object belonging to a class.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161