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 trialYong Li Soh
Courses Plus Student 2,555 PointsWhen to Import Interface, implement or do both?
I realized that "import java.io.Serializable;" is followed by "implements Serializable", "import java.util.List;" NOT followed by "implements List", Comparable<T> was NOT imported but implemented.
When do we implement an interface, import or do both?
1 Answer
rtholen
Courses Plus Student 11,859 PointsWhen you see a statement like "import java.io.Serializable", it means that somewhere in your code file you intend to "use" this class in some manner. You can then refer to it in a shortened form as "Serializable". If you fail to have the import statement you can still use the class or interface in your code but must refer to it in its fully qualified form as "java.io.Serializable". When you "implement" an interface, you provide code that implements the "empty" member functions of the specified interface. An interface simply specifies what it does, not how it is accomplished. When you provide an implementation you determine how it is done. I suspect List was not followed by "implements List" because your code example has something like "List myList = new ArrayList();" If you look at ArrayList in the Java API documentation, you will see that it "implements the List interface". Comparable was not imported because it is part of the default java.lang package, which is imported by default.
Yong Li Soh
Courses Plus Student 2,555 PointsYong Li Soh
Courses Plus Student 2,555 PointsThanks rtholen, I understand it better now!