Why is Java Comparable
used? Why would someone implement Comparable
in a class? What is a real life example where you need to implement comparable
The fact that a class implements Comparable
means that you can take two objects from that class and compare them. Some classes, like certain collections (sort function in a collection) that keep objects in order rely on them being comparable (in order to sort you need to know which object is the "biggest" and so forth).