title: Cast to inherited class from ArrayList of base class
body: <p>I have two classes A and B , where A is the base class and B is inherited from A.</p>
  
  <pre><code>Class A
  {
      int foo=10;
  }
  
  Class B extends A
  {
       int bar=100;
  }
  </code></pre>
  
  <p>I declare an ArrayList of Class A and insert Class A and Class B objects into it.</p>
  
  <p>My issue is when i attempt to get the Class B object which is in the ArrayList of Class A and then cast it to Class B as follows </p>
  
  <pre><code>B foobar = (B)ArrayListOfClassA.get(IndexOfClassB).
  </code></pre>
  
  <p>The value of bar in Object B is undefined. </p>
  
  <blockquote>
    <p>How do i get the value of Object B from the ArrayList of Class A ???</p>
  </blockquote>