collections - Polymorphism in Java -


why uses polymorphism in java while using collections.

what difference between these initializations.

  private list<order> orderlist = new arraylist<order>();   private arraylist<order> orderlist = new arraylist<order>(); 

in second form, orderlist can treated arraylist<order> or inheriting arraylist<order>.

in first form, orderlist treated list<order> , recommended when use functionality defined list<t> interface. way code more flexible , can refactored (e.g. realize need linkedlist<t> instead, change initialization , of code not need changed).

also, helpful code refactoring use interfaces instead of actual types, if possible (e.g. use parameters of type list<t> instead of arraylist<t>), because allows caller change list type without changing in called function.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -