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

luis martinez
luis martinez
2,480 Points

Why does he do Object obj.

I don't understand why he does Object obj. I really still don't understand how objects works or what are they. I understand the other stuff but the whole topic of Objects is really holding me back. Can someone please help it would be great.

@Override
public int compareTo(Object obj){
Treet other = (Treet) obj;
If (equals(other))
{
return 0;
}
int dateCmp = mCreationDate.compareTo(other.mDescription);
if (dateCmp == 0)
{
return mDescription.compareTo(other.mDescription);
}
return dateCmp;
}

[MOD: added ```java formatting -cf]

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey,

you are very welcome luis!!!!

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi luis,

object oriented programming is hard to understand at the beginning. Let me tell you what I got so far:

Everething is object in Java. If you have a Treet class you define methods and variables for that Treet. You can use this methods and variables if you create an object of the Treet class. This object is also called instance of the Treet class. This instance can use the methods and variables of the parent Treet class.

The code example is not easy to inderstand so I commented it and you can go line by line:

@Override
// we write the @Override because we implemented the Comparable interface
// if you implement an interface this interface has "empty" methods you need to define for usage
// interface is a contract, so we need to define the methods from that interface how they need to behave

public int compareTo(Object obj){

// this compareTo method thakes an object as argument (obj)
// in java everething is an objectr except primitive types like int, double and so on...
// so if you call compareTo method you NEED to give that methot an object
// so everething except primitives
// a Treet object for example

Treet other = (Treet) obj;
// in this part we convert our argument object  to Treet
// we gave the compareTo method an object of the Treet class, not a Treet
// we know that obj is a object of Treet but the compiler doesnt know it
// so we need to tell the compiler that obj is a Treet through this line of code above

If (equals(other))
// this is tricky here 
// other is our argument (object of the treet class ) that was converted into Treet 
// equals is comparing the Treet class itself with the Treet object we use as argument 
{
return 0;
// if the argument Treet is equal to the Treet class itself
// return 0
// it means that the Treets are equals to each other
}
int dateCmp = mCreationDate.compareTo(other.mDescription);
// if not equals
// we compare the Date and description of the Treets
if (dateCmp == 0)
{
return mDescription.compareTo(other.mDescription);
}
return dateCmp;
}
luis martinez
luis martinez
2,480 Points

Thank this was very helpful I am starting to understand what object are.

Treet other = (Treet) obj; // in this part we convert our argument object to Treet

In this method does he use "type casting" to on obj. (Treet) obj, does that mean he converted obj to Treet?