java - List initialization approach -


this question has answer here:

what difference between these 2 initialization of list :

list<integer> aa = new arraylist<>(); arraylist<integer> aaa = new arraylist<>(); 

polymorphism

the first uses polymorphism assign object reference variable type interface list. preferable.

in oop, practice assign objects variables of general superclass or interface. if calling code needs methods defined part of list, , not additional methods defined in arraylist, go list.

this gives code freedom switch different implementation of list, class other arraylist, without breaking calling code.

this basic java 101 concept should mastered sooner rather later on java education.


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -