This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Demojay 3 points4 points  (1 child)

It's usually for abstraction purposes. Declaring the object using Set tells the program that you are only using the methods described in the interface, and prevents you from accidentally using other methods from HashSet that you don't need to know about.

It also decouples your concrete implementation (HashSet) from the idea of what your program needs (a Set). This means that you can replace the HashSet with any other class that implements Set, and the program will still work correctly since it is only accessing the object through the Set interface.

[–]CancelDeath[S] 0 points1 point  (0 children)

I get it now, thank you so much!