Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Daniel Kwiatkowski
Daniel Kwiatkowski
1,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?

1 Answer

Kayondo Martin
Kayondo Martin
7,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.