Java Java Data Structures Organizing Data Interfaces

Script Ninja
Script Ninja
15,003 Points

Some questions about the interface video.

Hi, I dont fully understand the entire video that is why i want to walkthorugh this code. I want to walkthrough the code:

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

so as far as i understand we use interface to sign an imaginarry contacrt that apply custom rules. i want to sort my treets in some way and as far as i understand interfaces lets me to choose how, could have sort them by the name of the autor but we decided to sort them by the date they were created in order to know which treet come first, THIS IS WHERE MY QUESTIONS BEGINS!:

1)Object is the superclass for all objects in java and because of that we set the paramter in our compareTo method to Object?, we do it because it lets us to pass any object we want to compare to something?

2.after that we need to cast "object" to be a Treet because we want our obj to inherit all the properties from the Treet class and we assign it the variable "other" in order to gain access to the casted object? Am i right?

3.I didnt understood at all this line of code:

  if (equals(other)) {
  return 0;
    }

what exactly this line of code does?, do this line checks if our "other" variable is equals to the object parameter we set on our compareTo method?, i mean we have 2 instances of treet in our Example.java file, this line check if they are exactly the same and if they are the same why WE RETURN 0?

4.

  int dateCmp = mCreationDate.compareTo(other.mCreationDate);

in this line we check if mCreationDate is equals to the passed parameter in our comapreTo method which is other as we casted obj to other? but what exactly is mcreationDate contains?, it is contains our first instance of treet?

  1. why we need to type @Override? above the method? what exactly we overridng here? ina previous videos we used this to override the need to call a method to output a string but what is the usecase in here?

Steven Parker Eric McKibbin Jennifer Nor-dell Daniel Turato

sry for all the questions but i really want to understand the concept of interfaces, any insights will be appriciated

4 Answers

Tonnie Fanadez
Tonnie Fanadez
UX Design Techdegree Graduate 21,841 Points

Noob as usual pardon my bad grammar.

(1) Even if Object is the super-class of all classes I think you cannot pass just any Object you can think of because this will break your code; besides compareTo(T o) method requires you to explicitly specify the Object Type to be compared to. It is this Object Type that you specify that the obj parameter will be casted to. So any other Object Type is ruled out of the compareTo() method.

Treet other = (Treet) obj;

E.g. when you cast the obj parameter as a Treet Object you automatically lock out any other type of Object Classes.

(2) You're absolutely right on this; once obj has been casted as a Treet then the object can access Treet methods and state.

(3) if (this.equals(other)) equals() just checks if two Treets are equal to each other (true equality) wheres the compareTo() is used to identify the natural order of the Treets in terms of Date Created or the Description.

(4) The class Treet implements Comparable Interface (to enable sorting)which basically compares between 2 instances of the Treet Object. Our current Treet class is abstract as in it is empty and it doesn't contain any Treet Content or Data to work on. The Treet contents are supplied in the Main Class by writing a series of treets (on the video I believe Craig received a total of 58 treets ). The magic happens when Arrays.sort (treets) method is called; behind the scenes it breaks the treet array into different sub-arrays. Each sub-array is sorted in different threads so that sort can be executed in parallel fashion and eventually the array is merged finally as one sorted array of treets. In these sub-arrays there is the current treet say at index 0 and the next treet say index 1 which are subjected to compareTo () method.

Tonnie Fanadez
Tonnie Fanadez
UX Design Techdegree Graduate 21,841 Points

Overriding Annotation

@noobdeveloper you realize that public int compareTo(Object obj) is not a method of class Treet. This method is from Interface Comparable<T> which class Treet is implementing. Likewise public String toString() method doesn't belong to Treet class; it is from the Object Class, but because Treet is an Object the method is directly accessible or inbuilt on the Treet Class

So when we borrow (implement an interface method) we have to mark it using @Override annotation.

We mark the overridden method with @Override annotation to take advantage of the compiler checking to make sure we are actually overriding the exact method as specified on the contract. If you happen to misspell the method signature or leave out some parameters, the compiler or IDE will alert you.

Cheers