I am trying to use template pattern to define a generic algorithm in java. But the method that needs to be overridden, takes an object as input. This object will vary depending on the concrete implementation.
So I decided to use inheritance in input parameter. I am passing parent object in the overridden method of the abstract class. In the overriding method of concrete class I downcast it to the child and get the required details. It is said that downcasting is a code smell. How can I achieve this without downcasting. Or is there any other better solution.
Example code that shows the issue:
abstract class GenericAlgorithm {
void someMethod(GeneralParameter gp);
}
class ConcreteAlgorithm extends GenericAlgorithm {
@Override
void someMethod(GeneralParameter gp) {
// I can't take a SpecificParameter as argument,
// so I would need to downcast the parameter
SpecificParameter sp = (SpecificParameter) gp;
...
}
}