Java generic Comparable where subclasses can't compare to eachother

Posted by dege on Stack Overflow See other posts from Stack Overflow or by dege
Published on 2010-03-16T21:22:02Z Indexed on 2010/03/16 21:51 UTC
Read the original article Hit count: 335

Filed under:
|
public abstract class MyAbs implements Comparable<MyAbs>

This would work but then I would be able to compare class A and B with each other if they both extend MyAbs. What I want to accomplish however is the exact opposite.

So does anyone know a way to get the generic type to be the own class? Seemed like such a simple thing at first...

Edit:

To explain it a little further with an example. Say you have an abstract class animals, then you extend it with Dogs and ants.

I wouldn't want to compare ants with Dogs but I however would want to compare one dog with another. The dog might have a variable saying what color it is and that is what I want to use in the compareTo method. However when it comes to ants I would rather want to compare ant's size than their color.

Hope that clears it up. Could possibly be a design flaw however.

© Stack Overflow or respective owner

Related posts about java

Related posts about generics