Usually method overloads delegate their parameters to the more detailed overloads with default values. here is an example

A(x) => A(x, null);
A(x, y) => A(x, y, null);
A(x, y, z) => ...;

What should I do if i want to override this method and do a little modification before calling the base method?

If I override only the most detailed overload, then I'm relying on implementation details of parent. if later, parent doesn't delegate all overloads to the most detailed overload then my implementation breaks (doesn't do what I intended).

override A(x, y, z) => base.A(foo(x), y, z);

If I override all overloads then I have to copy default parameters used in the base method. my implementation doesn't break, how ever it can be different from parent later on.

override A(x) => A(x, null);
override A(x, y) => A(x, y, null);
override A(x, y, z) => base.A(foo(x), y, z);

which one is correct?


This also depends on the language. Not all methods of the base class may be overridable. For a language which provides non-overridable methods, I would expect only the most detailed overload to be overridable.

When all methods are overridable, the most future-prof approach is to override them all.

| improve this answer | |

How about option 3, where you don't have any public virtual members, and they all call a protected virtual DoA(x, y, z)?

Then you can't get into the case where changes in the base break the subclass.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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