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
  • 10
  • 21
  • 2
    You might want to mention what language you are using, since that completely determines the answer. – psr Feb 07 '13 at 18:42
  • 2
    Add a line to your API doc saying "the implementation of this class is very complex. You should not override any functions in derived classes unless you fully understand how the entire class works." – us2012 Feb 07 '13 at 18:45
  • 2
    I upvoted your question because I think it exposes a problem in how you're thinking about this matter. And I think that answering that "hidden" problem makes for an interesting question. Thiton's answer is a very good one and addresses what I would say your actual problem is. –  Feb 07 '13 at 18:52
  • If you go ahead and implement what you are describing the implementation is language dependent (and may be impossible). However, the general answer was answered correctly by Thiton. But, almost never say never and all that, on the remote chance that *this* time it's a good idea it depends on the language. And after all that, for PHP I don't know the answer. – psr Feb 07 '13 at 19:06
  • By "other programmers", do you mean co-workers? Or do you mean unknown present and future users of some code that you are distributing (a product, or open-source project)? – Carson63000 Feb 07 '13 at 21:28

1 Answers1

18

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
  • 1
  • 27
  • 26
  • 1
    +1 for an excellent answer. You spotted the real issue behind the question and answered that well. –  Feb 07 '13 at 18:53