title: Why a super class object couldn't handle a child class list
body: <p>I have an abstract super class called <code>Recipient</code> and two child classes <code>OfficialRecipient</code> and <code>PersonnelRecipient</code>. <code>OfficialRecipient</code> has a child class <code>OfficeFriendRecipient</code>. I also have defined class <code>InputOutput</code> which has a method <code>UpdateFile</code>. Here <code>Recipient</code> class has methods <code>getName ()</code>, <code>getEmail ()</code>, and <code>OfficialRecipient</code> has method <code>getDesignation ()</code>.</p>
  
  <p>I have an array list defined as follows:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  List &lt;OfficialRecipient&gt; official = new ArrayList&lt;&gt;();
  </code></pre>
  
  <p>When I try to write the following code, java is showing error. The error is rectified when I cast pointer to <code>OfficialRecipient</code>.</p>
  
  <pre><code>Iterator &lt;Recipient&gt; pointer = official.iterator();
  while (pointer.hasNext()) InputOutput.UpdateFile("official", pointer.getName (), "null", pointer.getEmail (), pointer.getDesignation (), "null", filePath);
  </code></pre>
  
  <p>My question is why a superclass pointer could not handle a subclass <code>official</code> list. Isn't it what polymorphism supposed to do?
  I've just started studying java and couldn't understand this concept. Please briefly explain.</p>
  
  <p>The code compiles correct when writing like this:</p>
  
  <pre><code>Iterator &lt;OfficialRecipient&gt; pointer = official.iterator();
  while (pointer.hasNext()) InputOutput.UpdateFile("official", ((Recipient) pointer).getName (), "null", ((Recipient) pointer).getEmail (), ((OfficialRecipient) pointer).getDesignation (), "null", filePath);
  </code></pre>