I have an abstract class abstract class MathFunc that is used to implement the mathematical functions Rastrigin, Griewangk and Rosenbrock in their specific classes final class Rastrigin extends MathFunc, final class Griewangk extends MathFunc and final class Rosenbrock extends MatjFunc.

This hierarchy of classes and how they are defined so far works flawlessly so I think there is no need to look for improvements on this area.

Now I have to implement another class Generation that will have some ArrayLists in it but the thing is I need to have a Generation implementation for each mathematical function described above.

So that I need something like:

ArrayList<Rastrigin> rast = new ArrayList<>();
ArrayList<Griewangk> grie = new ArrayList<>();
ArrayList<Rosenbrock> rose = new ArrayList<>();

Inside these lists I have some Rastrigin/Griewangk/Rosenbrock objects that I need to instantiate.

I know from the past projects in C++ that I can use templates to specify a generic data type and I'm thinking that this is my solution. My implementation so far looks like this:

public class Generation <MathFunc> {
    private final ArrayList<MathFunc> pop = new ArrayList<>();
    private final ArrayList<MathFunc> nextpop = new ArrayList<>();
    private final ArrayList<MathFunc> Crossover = new ArrayList<>();

    Generation(MathFunc tp)
        for(int i = 0; i < PopSize; i++)

But the problem is: can I use the abstract class to instantiate the objects that rely on it? Is there another way to do it? I'm quite confused about the usage of templates.

But doing Generation(Mathfunc tp) seems a little weird to me since abstract classes are not instantiable.

  • You may want to adjust your question, as you're not as much asking for the design of a template class as you seeking aid on using its derivations generically.
    – DougM
    Dec 11, 2013 at 16:52

3 Answers 3


To answer a bit of what you are asking: you can use the abstract as the declaring type, the type of your reference, but the (real) object should be that of a concrete/implementing type. Probably you already got that; indeed, in this case, probably using interfaces would be a plus.

To help some more, a nice tutorial on the template method is here: http://www.oodesign.com/template-method-pattern.html. Also, please read a bit about Java generics usage: http://docs.oracle.com/javase/tutorial/java/generics/types.html.


You don't need to instantiate the Mathfunc class. Instead, what you can do is to instantiate one of MathFunc's sub classes and treat the object as an instance of Mathfun. For example:

MathFunc math = new Rastrigin();
Generation g = new Generation(math);
  • Well I guess that abstract classes are not instantiable. If it were an interface it would work I guess.
    – Edeph
    Dec 11, 2013 at 9:27
  • 1
    @Edeph - Interfaces are not instantiable either. Dec 11, 2013 at 20:03

I managed to solve the problem as stated here. It looks like factory pattern would work best in this scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.