Return type of Arrays.asList is List and casting it to ArrayList is throwing an error. ArrayList is a child implementation class of List.So, Casting List to ArrayList will be downcasting. Then why below 1st line is throwing Run time error.
ArrayList<String> list = (ArrayList<String>) Arrays.asList("Amit","Suneet","Puneet");// Run time Error
List<String> list2 = new ArrayList<>(list);
Error:
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Can't we perform downcasting with interface and implementation class? If can then how?