We redefine the definition of a Collection
A collection, sometimes called a container, is simply an object that groups several items into one unit. Collections are used to store, retrieve, process, and share aggregate data . Typically, they represent data elements that form a natural group .... (continued).
Look at the bold words. That should give you an answer.
If you put objects in an ArrayList that do not belong to the same implementation, you are certainly causing problems in situations where you want to retrieve objects from a list and infer them into the appropriate types. Therefore, you must avoid to do this.
Consider that you store Dog and Car objects in an ArrayList . Then for each saved object you want to call some method, say barkLoudly() . Now this method will work perfectly with the Dog object. But when it is called on Car , boooom .... An Exception will appear.
Shaggy d
source share