Hello fellow ranchers, I've been puzzled how to implement Comparable regarding inheritance..
Lets say we have List defined as superclass and we wish to sort it using Collections.sort(List<T>), but meanwhile List contains both instances of superclasses and subclasses. Let's also assume that SubClass adds some field, which is relevant to the comparison process, but (of course) only when comparing subclasses. Therefore we need to make instanceof tests and cast objects to subclass when applicable..let's also say that Subclasses come "first"..example
java code follows..
Now this *seems* to give correct results, but from my point of view it's ugly code. How comparing lists which may contain both subtypes and supertypes should be implemented? (Without Comparator). Surely my implementation can't be satisfactory? Or should we provide *some* hook / callback method which would then be called on compareTo-method? How should one solve this in a satisfactory way?