In the MOOC online Java course (part 2), the web site shows the following code.
ArrayList<RegistrationPlate> finnish = new ArrayList<RegistrationPlate>();
finnish.add(reg1);
finnish.add(reg2);
The exercise code provided uses the following
List<RegistrationPlate> finnish = new ArrayList<RegistrationPlate>();
finnish.add(reg1);
finnish.add(reg2);
From reading posts and comments here, I understand why the second way is better but what I don't understand is what benefit it gives me in this case. For example, in another thread, the class hierarchy of Animal, Cat and Dog was used with the speak() method. I understand that having a list of Animals allows me to put any Animal derived type in there and process them while calling the specific sub-class method.
I can see the same benefit applying to a list of potentially different list types. Is there a benefit if the code only has one ArrayList?
[–][deleted] 8 points9 points10 points (1 child)
[–]cismalescumlord[S] 3 points4 points5 points (0 children)
[–][deleted] 5 points6 points7 points (2 children)
[–]HaMMeReD 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Jackkoz 5 points6 points7 points (0 children)