That is how Generics work. You cannot cast or convert a generic type, only the base type.
So you can assign an ArrayList<String> to a List<String> reference variable, but you can never assign an object with one generic type to an object with another generic type, no matter what inheritance there is.
This is because generics don't exist at runtime, and if the compiler would allow you to do that, your program could very well crash.
It is explained well in K&B's Study Guide.
Imagine you put your ArrayList<String> into a List<Object>. Then what you do is you put an Object into your List<Object> reference. But your List<Object> is still, for all intents and purposes an ArrayList<String>! So what you're doing is you're putting an Object into a String ArrayList and your compiler can't catch it because you're using a List<Object> reference. And at runtime, generics don't exist! Next thing you know, your program explodes and your users are full of gory bits and bloodspatter.
A List<Dog> is not a List<Animal>. Consider what you can do with a List<Animal> - you can add any animal to it... including a cat. Now, can you logically add a cat to a litter of puppies? Absolutely not.
So in your case, you can't assign a ArrayList<String> to a List<Object>
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
because in case of List<Object>......it can contain anything which is an object which can be or not be same as a string object.......it may be down to any herarchy of object so, compiler will be comfused
but if you really wanna do that....then try this......
List<? extends Object> =listString
this says anything which is a subclass of object can refer to String Object.......
As String also extends Object so it is also a subclass of Object,so both are at same level of herarchy......
List<? super Object> s = null;
this means any class that is the subclass of object can have null as a refernce.....
this is as simple as saying.