I've always wondered what are the benefits of this type of instantiation of a list in java that I see in a lot of code
List<Object> objList = new ArrayList<Object>();
as opposed to
ArrayList<Object> objList = new ArrayList<Object>();
Both are valid ways to instantiate a Java ArrayList object but why use the first one?
Why should objList be casted as the interface rather than the actual class that implements it? What are the cases where this is helpful?
Also, on compiler level, when we invoke a method on objList that is specific to ArrayList but not the interface List, roughly speaking, how does compiler "know" that method belongs to ArrayList when objList is casted only as a generic obj that is an instance of some class that implements List?