user2876831 February 2016

how comparable interface return int value, if an interface having only method declaration

String s1="AAA";
String s2="BBB";
int result_int = s1.compareTo(s2);

The result_int will be -ve value, It means .compareTo method says s1 is lesser than s2. My question is=> if .compareTo method is from Comparable interface,then an interface method how should have body?? if compareTo method doesn't have body, How it will return integer value while passing two objects.

Answers


Nikitha Reddy February 2016

CompareTo method does have an implementation. The method is implemented in all the classes such as Integer, Double etc. In your case you are using String's compareTo method.

If you want it to be customized as per your class, you do so by overriding the compareTo method.

Post Status

Asked in February 2016
Viewed 2,610 times
Voted 10
Answered 1 times

Search




Leave an answer