Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
You might want to mention what language you are using, since that completely determines the answer. – psr Feb 7 '13 at 18:42
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 7 '13 at 18:45
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. – GlenH7 Feb 7 '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 7 '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 7 '13 at 21:28
up vote 18 down vote accepted

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.

share|improve this answer
+1 for an excellent answer. You spotted the real issue behind the question and answered that well. – GlenH7 Feb 7 '13 at 18:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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