Welcome to the Treehouse Community
Looking to learn something new?
Daniel Kwiatkowski1,576 Points
interface vs implementation
Why is it in the tutorials we do an interface and then a implementation? For example. Set<String> obj = new HashSet<String>(); If Set<String> obj is an interface and new HashSet<String>() is the implementation
and outside team treehouse I see, HashSet<String> obj = new HashSet<String>(); HashSet<String> obj is an interface? and an implementation? In terms of thinking of interface and implementation, why have something like this in an explanation if interface and implementation are set to be different? Is there something I am missing?
Kayondo Martin7,481 Points
Set is the interface, HashSet is one of the classes that implement Set. You can not have an interface instantiated directly, so outside treehouse, HashSet<String> Obj = new HashSet<String>(); is a direct HashSet instance. whereas in treehouse, Set<String> Obj = new HashSet<String>(); Obj is a HashSet but as object of the Set interface. So, because Set is implemented by HashSet, this is also still allowed.