Implicit type parameters

S

SoulSpirit

Let's suppose we have an abstract class who wants to force its
subclasses to implement Comparable in a way that the extending class
has the ability to be compared only to objects of its same type.
The solution I can figure out is the following:

public abstract class Animal<T extends Animal> implements
Comparable<T>{
public abstract int compareTo( T other );
}

public class Dog extends Animal<Dog>{
public int compareTo( Dog other ){
return 0;
}
}

But it makes me doubtful about the necessity to declare the extending
class with a type parameter (Dog) that is the same as the class name.
I feel like I could eliminate the repetition (Dog-Dog) changing
something in Animal declaration, obtaining something like this:

public class Dog extends Animal{ //Notice, there isn't <Dog>
public int compareTo( Dog other ){
return 0;
}
}

Is it possible?
Thanks
 
I

Ingo R. Homann

Hi,

Is it possible?

No.

IMHO, this has something to do with the Liskov-substitution-principle:
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.

I think, your first solution is the correct one.

Ciao,
Ingo
 
S

SoulSpirit

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
 
E

Eric Sosman

[...]
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top