Hi,
I studied polymorphism and understand that it can do dynamic method binding like below.
Assuming that class Animal is abstract class.
public class AnimalReference
{
public static void main(String args[])
Animal ref // set up var for an Animal
Cow aCow = new Cow("Bossy"); // makes specific objects
Dog aDog = new Dog("Rover");
// now reference each as an Animal
ref = aCow; ref.speak();
ref = aDog; ref.speak();
}
I used to create instance of ArrayList like:
ArrayList myList = new ArrayList();
But usually I figured that people write:
Collection myList = new ArrayList();
So my confusion is what is the benefit of declaring as Collection? Also I didn't know you can have "Collection" (which is an interface not abstract class) in front of "myList".
Why it is not good practice to just say:
ArrayList myList = new ArrayList();
I read Collection interface and ArrayList Java documents as well as online tutorials but still not really clear.. Could anyone give me some explanation?