1. List<Car> carList = new ArrayList<Sedan>();
2. List<Car> carList = new ArrayList<Car>();
carList.add(new Sedan());
1 has a compiler error and 2 is legal.
Why the type of variable declaration must match the type we pass to the object type(derived type is not allowed)? I was using Array as following which absolutely correct:
int SIZE = 10;
Car[] carArray = new Sedan[SIZE];
Can anyone tell me why collections must declare as condition 2? Thanks