I'm a bit confused about the casting rules for the collections API.
If I have an interface, a class that implements that interface, and a method that declares that it returns a list of such interfaces (but which builds a list of instances of the concrete class), then I thought that I should be able to call the method and cast the result of the method to a list of the concrete classes.
Not sure if the above is totally clear, so the following simple code illustrates what I mean:
This all compiles fine. But when I introduce the code below, then I can an "incompatible types" compilation error.
I thought that this should be legal. I'm using the 1.6_027 version of the JDK (64 bit version of windows 2008 server). If I use Jdeveloper and the Oracle
Java Compiler (jdeveloper 10.1.3.5) then it compiles and works fine.
Also if I change the client call so that it missing out the types, i.e. to: List myList = service.getListModelObject();
then it compiles and runs OK and I can access the list via getItem() and cast the result of this to ModelObject fine.
Can anyone enlighten me as to what is going on? Am I (and the oracle Java compiler) wrong or is there something up the the JDK javac?
Thanks,
Andy
[Added code tags - see UseCodeTags for details]