title: Java: Meaning of non-generic pointer pointing at an generic object?
body: <p>I'm quite new to Java (and english), so please bear with me.</p>
  
  <p>Tried to write something like...</p>
  
  <pre><code>Container con = new Container&lt;Book&gt;();
  con.insert(new Book());
  con.insert(new Car());
  </code></pre>
  
  <p>...and did not get any type of error. But lines like...</p>
  
  <pre><code>Car c = con.remove(); // removes the last inserted element for simplicity
  </code></pre>
  
  <p>said "error: incompatible types", so I changed it to</p>
  
  <pre><code>Object carObj = (Car) con.remove();
  </code></pre>
  
  <p>and it worked. My problem is: when I say</p>
  
  <pre><code>new Container&lt;Book&gt;();
  </code></pre>
  
  <p>I create a container that can only hold objects of type Book, but because of the pointer (which is non-generic?) I can suddenly put any kinds of objects in my container. What happened here? The pointer only sees the Object-personality in whatever is in the Container, but I didn't know the pointer allowed every object with Object-personality in a container mainly created as generic (my formulation might be wrong). So when I have a non-generic pointer, it doesn't matter whether I create a generic or non-generic container? It will always be considered as a non-generic container (where I have to cast objects when I remove them)?</p>
  
  <pre><code>new Container&lt;Book&gt;().insert(new Car()); // compiler error as excepted
  </code></pre>
  
  <p>Got curious and made the problem even worse (maybe).</p>
  
  <pre><code>Container&lt;Car&gt; cars = new Container();
  cars.insert(new Book()); // compiler error: required Car, found Book
  </code></pre>
  
  <p>Now the pointer only sees the Car-personalities in the container. But it won't allow me to put in a book even though I created the container as non-generic. Why?</p>
  
  <pre><code>new Container().insert(new Car()); // works fine
  </code></pre>
  
  <p>Must say, it's both fascinating and irritating...</p>