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 Organizing Data Interfaces

Majed Nabil
Majed Nabil
2,180 Points

about casting

I have a question, when Mr.Craig casts the obj, I do not know where is the obj defined in the program, can anyone help in this?

1 Answer

Sławomir Lasik
Sławomir Lasik
7,792 Points

Well, it isn't Not now anyway... In the compareTo(...) method you only know that you, other programmer, or for example sorting program (which sorts the Treet objects) will try to compare Treet object to other Object which should be Treet. (It is pointless to call treet.compareTo(SomeString) isnt't ? it will fail anyway). You and others should do that with compareTo(...) method. How? You call the compareTo(...) method on the Treet object giving the other object that You want to compare to. You, other programmer, sorting program should call the compareTo(...) method giving the other Treet object that You want to campare to.

In the example shown by the Proffesor, you see, that compareTo() method is needed by the sorting algorythm. That's when this method is called and the Treet object is passed.