title: How does Dynamic (run-time) polymorphism work in java? In other words how does the JVM know which methods to call?
body: <p>Say you have an arrayList of objects <code>ArrayList&lt;Object&gt; arr = new ArrayList&lt;Object&gt;();</code> and you then fill this arraylist with several different objects, all of which inherit from <code>object</code>.</p>
  
  <pre><code>arr.add(new Integer(1));
  arr.add("Im a String");
  arr.add(new SomeOtherObject);
  </code></pre>
  
  <p>Say you then loop through the arrayList calling the <code>.toString()</code> method, this will actually work as expected, calling the overridden <code>toString()</code> methods of the <code>String</code> and <code>Integer</code> classes, what confuses me is that to the JVM, the <code>Integer</code> and <code>String</code> objects inside of the arrayList were implicitly casted into objects, so how in the world does the JVM know to caste them back into whatever they were before they were put into the arrayList? Sorry if this question has an obvious answer, but this doesnt seem to make much sense to me</p>