reset password
Author Message
vsluong4
Posts: 87
Posted 00:11 Oct 15, 2014 |

If you have one function nested within another function, which function defines how the other one works?  For example let's consider the methods compare and compareTo.

 

public class myCustomClass implements Comparable<myCustomClass>{

..........

private String firstName, lastName, birthday;

private Integer age, birthday;

public int compareTo(myCustomClass other){

   if(this.firstName.compareTo(other.firstName) == 0)

      return 0;

   else if(other.lastName.compareTo(this.lastName) != 1)

      return 1;

   else return this.age.compareTo(other.birthday);

   }

}

 

Does the String/Integer compareTo define how myCustomClass.compareTo compares elements or does myCustomClass define how String/Integer compareTo compares elements?

Last edited by vsluong4 at 09:46 Oct 15, 2014.
rabbott
Posts: 1649
Posted 06:53 Oct 15, 2014 |

Not quite sure what you are asking. Are you asking about the body of the compareTo() function in your class? For example are you asking which compareTo the expression

... firstName.compareTo( ... )

uses?

If so, the answer is that it uses the compareTo() function defined in the String class since firstName is a String.

On the other hand, if you have an instance myInstance of your class and called myInstance.compareTo(...) it would use the compareTo() method you defined.

If that's not your question, would you try asking it again.

vsluong4
Posts: 87
Posted 10:25 Oct 15, 2014 |

Well I guess my question is which method defines how the other method compares elements.