0

I've got an abstract class PricingProblemSover:

public abstract class PricingProblemSolver<T,U extends Column<T,U>, V extends PricingProblem<T, U>> implements Callable<Void>{ }

And some classes which extend this solver, for example:

public class ExactPricingProblemSolver extends PricingProblemSolver<CuttingStock, CuttingPattern, CuttingStockPricingProblem> { }

Where CuttingPattern and CuttingStockPricingProblem are resp. defined as:

public class CuttingPattern extends Column<CuttingStock, CuttingPattern> {}
public class CuttingStockPricingProblem extends PricingProblem<CuttingStock, CuttingPattern> {}

Now I need a List of solverClasses:

List<Class<PricingProblemSolver<CuttingStock, CuttingPattern, CuttingStockPricingProblem>>> solvers=Arrays.asList(ExactPricingProblemSolver.class);

This list will be used by a factory class to produce a large number of instances of these solvers for each problem instance.

The above List code gives however the following error:

Type mismatch: cannot convert from List<Class<ExactPricingProblemSolver>> to List<Class<PricingProblemSolver<CuttingStock,CuttingPattern,CuttingStockPricingProblem>>>

I'm not sure why this goes wrong. Is there a way to fix this?

Joris Kinable
  • 2,232
  • 18
  • 29
  • This is a well-known... "behaviour" of Java. a `List>` is a different type than `List>>`, even if `ExactPricingProblemSolver` extends `PricingProblemSolver`. The only workaround i can imagine is: Make a `List>` and store your `ExactPricingProblemSolver<...>`s in that list. The list can take object of child-classes. – Turing85 Apr 12 '15 at 21:16
  • @Turing85 what do you mean by "behaviour". Are you suggesting there is some way to have type safe generics without PECS? – Boris the Spider Apr 12 '15 at 21:20
  • `Class` would be `Class extends PricingProblemSolver>`. – Bubletan Apr 12 '15 at 21:24
  • @BoristheSpider No, i do not suggest anything. But i am quite confident that it would be possible to support inheritance of generics within a language from the get-go, i.e. enabling the syntax OP wrote. Java just does not support it and thus "behaves" differently. – Turing85 Apr 12 '15 at 21:27
  • @Turing85 this has absolutely **nothing** to do with the implementation. If it a fundamental _requirement_ to ensure type safety. – Boris the Spider Apr 12 '15 at 21:28
  • @BoristheSpider of couse is has not. That is, why it was targeted to you. It was just to answer your question why i used the word "behaviour" =) – Turing85 Apr 12 '15 at 21:32

0 Answers0