Java won't consider it as the specific implementation type.
Consider the following case, where you can see where the problem will occur (IF automatic mapping of implemented class in the function argument is seen valid by Java):
public class CoffeeWorkflow implements IWorkflow
{
public void start(IWorkflowStartArgs args)
{
// This is what Java sees as actual implementation
}
public void start(CoffeeWorkflowStartArgs args)
{
// This is yet again SEPARATE method with different signature
// In case of auto-casting (if there would have been), this method would be AMBIGUOUS
}
}
The solution?
Well use Generics as illustrated by @sanbhat
Or if you don't want to go into Generics,
Then I think you should pass in an IWorkflowStartArgs and cast it inside as you said first,
like this way:
public class CoffeeWorkflow implements IWorkflow
{
public void start(IWorkflowStartArgs args)
{
if (args instanceof CoffeeWorkflowStartArgs) {
CoffeeWorkflowStartArgs coffeeArgs = (CoffeeWorkflowStartArgs) args;
// ....
}
}
// ....
}
Suppose you have another similar class TeaWorkFlow,
then again you need to check instanceof
.
This is why Generics were mainly introduced - To avoid repeatedly checking by instanceof
;
And to serve as a general model for similar pattern classes.