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 Java Data Structures - Retired Exploring the Java Collection Framework Sets

SortedSet added new method? How?

At one point, Craig changed the interface from set to sortedSet and somehow, after doing that the implementation (treeset) acquired a new method (called "headset()").

And interface is suppossed to be a "bunch" of abstract methods to be applied by the implementation, so...how can chaning the interface add a new method to the TreeSet class?

if the method is defined in the TreeSet implementation, it is available while using both interfaces....(or at least that's what I thought 'til right now!hehehe)

A long explanation to understand the logic behind this would be really appreciated! :)

2 Answers

first sorry if my poor language couldn't deliver the meaning So we have interfaces that have some methods like the interface (Set) which have add(), clear(), size() and other methods that's common and available to every class or interface that would implement or extend the set interface then we have (SortedSet) interface that extends the (Set) interface so right now SortedSet interface have access to all Set's methods and SortedSet add another methods like headSet() so you could say that class TreeSet not directly has access to SortedSet methods and of course the Set methods

so you can think of as inheritance every one down has what above him

-Set
---SortedSet
--------TreeSet

look at the documentation here https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#headSet(E)

Andrew Menning
Andrew Menning
10,050 Points

The only methods available to an object are determined by the reference type declared for the variable holding your object. You can experiment with this in jshell with the Treet class, which is a child of the Object class. If you create a new Treet but assign it to a variable of type Object, that variable will only have methods that are defined in the Object class. However, if you downcast this object into a Treet variable, you will then have all the methods of Object and Treet classes, such as getWords() from the Treet class. This is why the reference type of the variable holding the object needed to be change from Set to SortedSet in order to get access to the headSet and tailSet methods.

jshell> import com.teamtreehouse.Treet

jshell> Object obj = new Treet("author", "description", new Date());
obj ==> Treet: "description" by author on Tue Feb 20 18:50:07 EST 2018

# Object class doesn't have getWords defined, so you get an error.
jshell> obj.getWords()
|  Error:
|  cannot find symbol
|    symbol:   method getWords()
|  obj.getWords()
|  ^----------^

jshell> Treet treet = (Treet) obj
treet ==> Treet: "description" by author on Tue Feb 20 18:50:07 EST 2018

jshell> treet.getWords()
$11 ==> [description]