Can someone explain the following code to me. As in why is “List” and “ArrayList” are
not the same? Does this mean that the variable neighborColumns is a List data structure that holds type Column objects but only public methods from class List can be used on variable neighborColumns? thanks in advance.
List<Column> neighborColumns = new ArrayList<Column>();
This code is ok (Design to interface (List): –
In the above code, you are basically designing to an interface rather than implementation..List is an interface and ArrayList is a concrete class implementing it.
An advantage is that, you can switch between various implementation, without changing the design..
Below code is a problem (Design to implementation (ArrayList):-
Here since you are designing to an implementation.. So you can not change the implementation on the RHS.. The compiler will cry as above..
So, in general also, you should always design to an interface, thus not exposing the implementation, and also gaining flexibility of changing the implementation anytime..