Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7805283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:10:34+00:00 2026-06-02T02:10:34+00:00

Though I am not very new to Java, the following questions just confused me

  • 0

Though I am not very new to Java, the following questions just confused me so much that I cannot overcome this.
I searched a lot on web and found some answers too, but they weren’t completely satisfactory.Could anyone please clear my doubts

My confusions are:

 1) Vector v = new Vector();  
    v.addElement("A");  
    ........  
    ........  
    Iterator iter = v.iterator();  

Q1:Iterator is an interface and we cannot instantiate or create object to an interface.Then what should we call the “iter” here and what is it ?

Q1.1: All the methods of the Iterator(e.g. next()) are called by “iter“. Where are these methods defined?
If these methods are defined in a particular inner class of Vector class(as I found on web), what does
the iterator() method return ?Cann’t we call these methods directly by the object ‘v’?

Q1.2: According to Javadoc: Vector implements {Cloneable, Collection, List, RandomAccess, Serializable}
None of these implement Iterator.So how does Vector implements Iterator?

 2) In the declaration:  
    List list = new ArrayList();  

Q1: List is an interface and ArrayList is a class. So new ArrayList() creates an instance of ArrayList.
How can this instance be applied to “list” and AGAIN what is it?

Thanks…..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:10:36+00:00Added an answer on June 2, 2026 at 2:10 am

    Re 1) Iterators are indeed typically implemented as a private inner class of the corresponding collection class. That’s why you can’t see the actual implementation class, only the interface. And that is fine, since its implementation details are not the client’s concern. You can check its real type in e.g. a debugger though, or look it up in the source code of Vector, bundled with your JDK installation.

    Vector does not implement Iterator itself – it only returns an instance of the inner class mentioned above, from its iterator method. This inner class instance has a reference to the Vector instance that created it, and sees its internals, that is why it can iterate through its elements. This is why the Iterator pattern is useful: it allows you to iterate through a lot of different collections (or in fact Iterables), without needing to deal with their implementation details.

    Re 2) your list is exactly what it looks to be: an instance of the ArrayList class. The latter implements the List interface, thus every ArrayList is a List.

    List list = new ArrayList();
    

    is an example of the idiom “program for interfaces, not for implementations”. Since the declared type of list above is List, you can use it like any other List and your subsequent code isn’t dependent on its actual implementation. So if it later turns out that instead of an ArrayList, it would better to use e.g. a LinkedList or a CopyOnWriteArrayList, you only need to change the one line of code above, and the rest of your code will still work perfectly. Whereas if you declare list as

    ArrayList list = new ArrayList();
    

    and later realise its type should be changed, you need at least to go through all the code using list and change its type accordingly wherever it is mentioned. Furthermore, some of the code may even use methods specific to ArrayList, which aren’t available or work differently in the other implementation class, making the change even costlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following not-very-useful code: package com.something; import java.util.ArrayList; import java.util.Collection; //Not a generic
An issue I discover today is similar to this unanswered problem ; though not
Even though it's not part of HTTP 1.1/RFC2616 webapps that wish to force a
This is clearly not the case though. My JS : $(.job_charge.item-block).live({ mouseenter: function(){ $(this).find('.edit-and-delete').stop(true,true).fadeIn();
Possible Duplicate: Pointers in java Why java is not using pointer though C# and
Kryo is a very new and interesting Java serialization library, and one of the
I come up with this question when implementing singleton pattern in Java. Even though
Being new to Java, I am not able to understand the complete build process
I'm not exactly new to java (I've been using it for a year now)
This might be a very simple problem indeed, but I am absolutely new to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.