title: How to use iterator to retrieve objects from two classes in Java
body: <p>I am new to Java and learning about collections. I have a problem in retrieving data using an <code>Iterator</code> from two class objects, I am only able to get output for any single class but not two classes.</p>
  
  <p>This is my code:</p>
  
  <pre><code>package ncl;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.Iterator;
  
  
  public class Test {
  
  public static void main(String[] args) {
  
      Market m1 = new Market(1234, "Suryam", 1200.10);
      Market m2 = new Market(4321, "SWathi", 1200.10);
      Market m3 = new Market(1111, "Chinna", 1200.10);
      Market m4 = new Market(2222, "Malli Bava", 1200.10);
      Market m5 = new Market(3333, "Vali", 1200.10);
  
      RetailShop rt1 = new RetailShop("Swathi", 8121720217l, 500087);
      RetailShop rt2 = new RetailShop("Tumbu", 9989330442l, 500087);
      RetailShop rt3 = new RetailShop("Suri Babu", 9010627821l, 500087);
  
      ArrayList&lt;Object&gt; al = new ArrayList&lt;Object&gt;();
  
      // Adding Market
      al.add(m1);
      al.add(m2);
      al.add(m3);
      al.add(m4);
      al.add(m5);
  
      // Adding RetailShop
      al.add(rt1);
      al.add(rt2);
      al.add(rt3);
  
      Iterator itr = al.iterator();
  
      while(itr.hasNext())
      {
          Market mt=(Market)itr.next();
  
              System.out.println(mt.billamount);
              System.out.println(mt.customer);
              System.out.println(mt.invoiceno);
  
          RetailShop rts=(RetailShop)itr.next();
          System.out.println(rts.custName);
      }
  }
  }
  </code></pre>
  
  <p>Can someone tell me what I am doing wrong here? I want to print <code>Market</code> data along with <code>RetailShop</code> data</p>