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
Post a Comment