Why not List<String> listString = new ArrayList<String>();
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.
Above code gives compile time Error "Type mismatch: cannot convert from ArrayList<String> to List<Object>".
Note:
Object o1=new String(); -> Possible (Upcasting and Automatic in Java)
Stringo1=new Object (); -> Not Possible
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.
Above code gives compile time Error "Type mismatch: cannot convert from ArrayList<String> to List<Object>".
Note:
Object o1=new String(); -> Possible (Upcasting and Automatic in Java)
Stringo1=new Object (); -> Not Possible
0 comments:
Post a Comment