Search code examples
javainterfacecomparable

implementing Comparable in an interface


I am calling a specific class using only its interface. The problem is, the class itself implements Comparable, but because I am referring to the class via a different interface, the compiler does not know it implements Comparable. I'm sure there is an easy solution to this... but I just can't think of it right now.


Solution

  • Will everything that implements the interface also implement Comparable<T>? If so, I suggest you just make the interface extend Comparable<T>.

    Otherwise, you could just cast to Comparable<T> if you happen to know that in this case it will work. Of course, that loses some compile-time type safety, but that's the nature of the beast.