The implementation of java.util.ArrayList
implements List
as well as extends AbstractList
. But in java docs you can see that AbstractL
just wanna to complement answers to question 2
java.util.ArrayList a=Arrays.asList(stra);
compiler just knows the return type of Arrays.asList
is List
, but does not know its exact implementation which may not be java.util.ArrayList
. So
you got this compile time error.
Type mismatch: cannot convert from List to ArrayList
you can force an upper cast explicitly like this,
java.util.ArrayList a =(java.util.ArrayList)Arrays.asList(stra);
The code will compile successfully, but a runtime exception will happen,
java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
this is because java.util.Arrays$ArrayList
(the type of implemenation which Arrays.asList
returns) is not a subtype of java.util.ArrayList
.