5

I have a class in which I want to disallow other programmers from overriding one of it's methods, since it requires special knowledge of the inner workings of the class. Since I personally know how the class works, I would like to be able to make an exception and actually DO override the function in one derived class. Is this a common problem to which there is a standard solution, or does the fact that I feel like I have to make an exception like this mean I should rethink my approach?

Edit: I didn't mention languages, since I was looking for a general answer, but since it was requested, this came up using PHP.

DudeOnRock
  • 1,079

1 Answers1

17

You should rethink your approach. Put yourself in the shoes of the future maintainer of your class, be it future you (who has forgotten some stuff) or someone else.

You want as few "special" commentaries or magic behaviour in the code as possible. This is especially true for special inheritance, where base class codes makes undocumented assumptions on the derived classes. And in your case, the sentence "requires special knowledge of the inner workings of the class" means that knowledge about the base class is encoded in the derived class, and the derived class makes a lot of assumptions on the base class. If you have the desire to make the base class final-but-not-for-me, you seem to have the gut feeling that not all assumptions are readily understood. This is a nightmare for a maintainer.

If you need to derive, do so and derive openly, with all the associated costs. If it isn't worth the cost, don't derive. Intermediate solutions incur all the costs in maintenance, but none of the gains.

thiton
  • 5,348