abstract method called in an abstract class

Hello,

I am writing some code that I'd like to be as generic as possible.

I created an abstract class called Chromozome. This abstract class has a protected abstract method called initialize().

I also created an abstract class called Algorithm which contains a protected ArrayList<Chromozome>.

I would like to create a non abstract method (called initializePopulation()) which would create instances of Chromozome, call their method initialize() and full the ArrayList with them.

In a practical matter, only subclass of Algorithm will be used, using an ArrayList of a subclass of Chromozome implementing their own version of initialize.

I have been thinking of that and concluded it was impossible to do. But I'd like to ask more talented peaple before forgetting it !

Thanks,

Vincent

[844 byte] By [Vebe75a] at [2007-11-27 11:09:11]
# 1

Why is this impossible?

aria_kokoschkaa at 2007-7-29 13:33:50 > top of Java-index,Java Essentials,Java Programming...
# 2

Ok, let's it is not impossible, juste that I had no idea of how doing it :-)

The difficulty is that Algorithm will never have to deal with Chromozome itself, but always with subclass of Chromozome. This is usually not an issue, but in that case, Algorithm is required to create instances of the desired subclass of Chromozome, but without knowing in advance wich subclass will be used (I hope what I say makes any sense).

Actually I may have found a way in the meantime, but maybe not the best one.

I created in Algorithm an abstract method :

protected abstract Chromozome createChromozome()

The method initializePopulation will call createChromozome instead of calling directly the constructor and the initialize() method of Chromozome.

Then subclass of Algorithm will implement the method createChromozome using the desired subclass of Chromozome.

Vebe75a at 2007-7-29 13:33:50 > top of Java-index,Java Essentials,Java Programming...