Tuesday 15 May 2012

java - Generics runtime exception -


Can someone please explain to me why this would create a runtime exception? It seems that I'm specifically accessing an array of B series array lists

  class A {} class B expanded a {} list & lt; B & gt; BL = new arreelist & lt; B & gt; (); List & lt; A & gt; AL = BL; Al.ed (new A ()); BB = BL.Get (0); // runtime exception    

You should not be able to successfully compile your code There should be a compiler error on this line:

  list & lt; A & gt; AL = BL;   

Because list & lt; Subclass & gt; a list & lt; Superclass & gt; is not. This is not specifically allowed because of your efforts - Enter a superkus example in the list of subclass examples.

No comments:

Post a Comment