> Wherever you have a reference to an (not-generic) Animal, it should be
> possible to compare it with another Animal. This should be of course
> also true, if the Object to which the reference refers is of type Dog.
Thanks for your answer.
I can't disagree with your point of view, but we know real-world
applications are always an exception to every principle :)
In the simplified example i've omitted the purpose of my... dilemma.
The purpose is to give someone other a clear class to extends that
already do all work but specialized jobs, giving less space as
possible to the implementor.
In short, my superclass is always able to compare an Animal with
another one if they are of different type, but fine comparision is
needed when the two Animal(s) are of the same type. The real
superclass is much like this:
public abstract class Animal<T extends Animal> implements
Comparable<T>{
abstract int compareToInternal( T other );
public final int compareTo( T other ){
if( getClass() == other.getClass() )
return compareToInternal( other );
else
... Animal knows what to do here ...
}
}
And I'd like to force subclasses to implement this compareToInternal()
method that compares only instances of the same type (Dogs with Dogs
and Cats with Cats).
An implementation like this...
public class Dog extends Animal<Cat>{
int compareToInternal( Cat other ){
return 0;
}
}
... should be seen as an error by the compiler.
Bye
Eric Sosman - 30 Jan 2007 16:33 GMT
SoulSpirit@gmail.com wrote On 01/30/07 10:21,:
> [...]
> And I'd like to force subclasses to implement this compareToInternal()
[quoted text clipped - 10 lines]
>
> ... should be seen as an error by the compiler.
I don't think you can do that. An abstract class (or an
interface) can require its subclasses (implementors) to provide
methods with particular signatures, but it cannot prevent them
from providing other methods as well. Those other methods may
have the same names as some of the required methods (that's
called "overloading"), but they are distinct methods. Java
will allow the subclass to provide all of
int compareToInternal(String attached) { ... }
int compareToInternal(double trouble) { ... }
int compareToInternal(char broiled) { ... }
int compareToInternal(float aLoan) { ... }
int compareToInternal(int p, int q, int r) { ... }
... and there's nothing (as far as I know) you can do to
prevent it.

Signature
Eric.Sosman@sun.com