1

What is the difference between the 2 calls:

Set<Record> instances = new HashSet<Record>();  -  on one hand 
HashSet<Record> instances = new HashSet<Record>();  - on other hand
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
extinction
  • 31
  • 3

2 Answers2

0

In the first case, you can reassign instances to an instance of any subtype of Set<Record>. In the second case, you can reassign instances only to an instance of a subtype of HashSet<Record>.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
  • 1
    When you're answering these kinds of questions, consider the level of expertise of the OP. If he's struggling over interfaces vs. concrete classes, it's unlikely he's going to know what you mean by "subtype." – Robert Harvey May 28 '14 at 16:39
0

HashSet implements the interface Set. Both statements store the HashSet in the variable instances (there is no difference, both statements store the same data).

The first statements however stores the HashSet in a Set. This has some advantages, for example you could change HashSet to TreeSet without changing your other code.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76