I’m not sure if I convey the question well enough and I couldn’t find a better way to do this, as I am quite new to java.
I believe the best is by way of illustration, if I have a class
public abstract class Genome
{
abstract public Genome randomize();
abstract public Genome mutate();
abstract public Genome crossOver(Genome genome);
}
Can I make sure that its subclasses would always implement by using generics
public class GenomeSubclass extends Genome
{
public GenomeSubclass randomize();
// etc...
}
instead of the abstract methods provided in the contract?
The normal way would be to define it like this:
This is the approach used by Comparable (or at least most uses of Comparable) and Enum, too.
Of course, this does not avoid subclassing other subclasses.
Edit to detail my comment:
You can’t have both
and
while both are implementing the same Genome method. Every subtype of GenomeSubSubclass must implement the
crossOver(GenomeSubclass)method, and can’t restrain the argument further.Of course, you could provide
GenomeSubclasswith an type parameter of its own:but then you can’t really use this class directly without somehow going back to the raw type. (You would have to write
GenomeSubclass<GenomeSubclass<GenomeSubclass<...>>>, which is not really possible. Or I’m somehow mis-thinking here.)The principle idea here is that every Genome subtype being used as a type parameter
Twould be compatible to all its own subtypes. This means, we could have this:and then