Let's consider block of code:
List<List<Object>> list = new ArrayList();
list.add(new ArrayList<Object>); //Line 2
list.add(new LinkedList<Object>); //Line 3
list.add(new LinkedList<Date>); //Line 4
Ok:
As far as I know polimorphic assigment DO NOT work within Generics
Question: But in the line: 2, 3, 4 Is there List, ArrayList, LinkedList are not generic for collection with name
list
??Line 4 won't compile, regardless of Date is subtype of Object
Question: Back to the question 1, Is this means that
Object
andDate
is case of polymorphism and generics???
Can understand it. Please help.
And one more question:
List list = new ArrayList<String>();
list.add(new Integer(1));
System.out.print(list.get(0));
Output: 1
Will compile, means no type safety. Why? Even if List list
- raw type, and ArrayList<String>
parametrized type, any way is useless. Why?
> so why List
– java_user May 24 '15 at 17:52