Java Java Data Structures Organizing Data Interfaces

implementing an interface and overriding its method

i have this doubt that when we implemented the Comparable interface and override the method int compareTo().

in the overridden method we typecasted the object to type Treet but later in a condition where if the dates are equal we go on and compare the mDescription Strings we simply called the compareTo() method..

My doubt is during the second call, had recursion occured or the simple String.compareTo() method responded to call.

well from the execution of code it is understandable that for sure the second thing had happened cause if recursion had occurred then an error would have generated sating String can't be converted to type Treet..

My doubt is how did the compiler know which compareTo() to call???

1 Answer

Juraj Sallai
Juraj Sallai
7,187 Points

It is because you are calling date.compareTo(other.date); and variable date is type Date. So it uses Date.compareTo() method, not the compareTo() method we just created. You can check my comment here