java - Class instantiation - Abstract class or concrete class for variable -


my question today not problem-solving question, best-practice theory question. getting familiarized joda-time, , read in user guide (http://joda-time.sourceforge.net/userguide.html#interface_usage):

when working collections interface, such list or map hold variable type of list or map, referencing concrete class when create object.

 list list = new arraylist();  map map = new hashmap(); 

this struck me quite odd statement. in programming, i've held concrete class variables unless i'm programming api of sort. have helpful material explain reasons why preferential hold general/abstract class vs concrete class variable type?

to illustrate, wrote simple method use whenever need list of objects string separated commas:

public static <t> string csv(collection<t> list) {     boolean first = true;     stringbuilder sb = new stringbuilder();     for(object e : list) {         if(first)             first = false;         else             sb.append(", ");         sb.append(e.tostring());     }     return sb.tostring(); } 

here, of course, must use collection can pass in want, object[], linkedlist, etc.

but let's elsewhere i'm storing set of strings , want linkedhashset, create variable this:

linkedhashset<string> set = new linkedhashset<string>(); 

or

linkedhashset<string> set = new linkedhashset<>(); 

(because of changes in java 7)

but according joda-time's user guide, should this:

set<string> set = new linkedhashset<string>(); 

?

i don't see advantages. have helpful input/reading material?

thanks!

if work collection (for example) doesn't matter, how collection implemented - want use collection methods , nothing more. in case code pretty agile , can change collection implementation. if basic interface not enough - use concrete reference.

it oop basics - should work abstractions, not concrete classes if possible.

similar questions:


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -