Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.