Let\'s say I have existing code which I want to extend but also to avoid changing it as much as possible.
Somewhere around this code there is a method that receives
Why not simply call the method with a parameter of the correct type?
Base b = null;
if (flag) {
Derived d = new Derived()
Engine.method(d); // so the correct method will be used for Derived
b = d;
} else{
b = new Base()
Engine.method(b)
}
You could also consider reusing the method(Base b)
:
public void method(Derived d) {
method((Base)b);
...
}