title: How exactly do Generics work?
body: <p>While looking up (testing) information for another question, I came across something and had completely no clue why it was happening. Now, I know that there is no practical reason to do this, and that this is absolutely horrific code, but why is it that this works:</p>
  
  <pre><code>ArrayList&lt;Quod&gt; test=new ArrayList&lt;Quod&gt;();
  ArrayList obj=new ArrayList();
  test=obj;
  obj.add(new Object());
  
  System.out.println(test.get(0));
  </code></pre>
  
  <p>So, basically, I am adding an Object to an ArrayList of Quods. Now, I see how java would have no way of efficiently checking for this, because it would have to look through all of the references, which probably aren't even stored anywhere. But then why is it that get() works. Isn't get() suppose to return an instance of Quod, like it says when you put your mouse over it in Eclipse? If it can return an object that is only an object when it promised to return an object of type Quod, why can't I return a String when I say I will return an int?</p>
  
  <p>And things get even weirder. This crashes as it is suppose to with a run-time error(java.lang.ClassCastException error)(!?!?):</p>
  
  <pre><code>ArrayList&lt;Quod&gt; test=new ArrayList&lt;Quod&gt;();
  ArrayList obj=new ArrayList();
  test=obj;
  obj.add(new Object());
  
  System.out.println(test.get(0).toString());
  </code></pre>
  
  <p>Why can't I call the toString on an Object? And why is it fine for the println() method to call its toString, but not for me to directly?</p>
  
  <hr>
  
  <p>EDIT: I know that I am not doing anything with the first instance of ArrayList that I create, so it is essentially just a waste of processing time. </p>
  
  <hr>
  
  <p>EDIT: I am using Eclipse on Java 1.6 Others have said that they get the same results in Eclipse running java 1.8. However, on some other compilers, a CCE error is thrown on both cases.</p>