Question about declaring variables
Hi guys,
I have a basic question about declaring variables. I've read that when declaring a set, for instance, one wants to begin with the most basic declaration followed by something more specific, like so:
Set<String> my_set = new HashSet<String>();
Why is this better than doing the following:
HashSet<String> my_set = new HashSet<String>();
It seems like a trifle, and it may be, but I still don't understand the advantages of the first case.
Re: Question about declaring variables
Imagine a third party software vendor made a hash set that was far more efficient than the one you have. You could just swap it in like this:
import org.betterSofware.utilities.*
Set<String> my_set = new BetterHashSet<String>();
But if you had this..
HashSet<String> my_set = new HashSet<String>();
you would have to trawl through your code base to see where people had used my_set as a HashSet, e.g
HashSet<String> my_other_set = my_set;
because swapping in a BetterHashSet would invalidate such code, wherever it appeared.
Re: Question about declaring variables