Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Objective-C lacks private methods or java-esque final methods. This means that it is possible for a subclass to (accidentally) override some of the internals of a superclass.

How does one prevent overriding methods in Objective-C?

share|improve this question
This will be closed if you don't add further explanation. This really shows no effort on your part and doesn't frame the question in a way which it could be answered beneficially. –  Jimmy Hoffa Jan 15 '13 at 17:41
add comment

closed as not a real question by Robert Harvey, Walter, Martijn Pieters, gnat, Glenn Nelson Jan 15 '13 at 18:18

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

How does one prevent overriding methods in Objective-C?

You write in the documentation for your class something like:

Subclasses should not override this method.

Seriously. I copied that quote straight from the documentation for UIView.

Objective-C encourages a culture of doing by convention many things which would be enforced by a compiler in other languages, and this is one of those things. I'm sure you could play some games in with the runtime whereby you get the IMP for the method you're about to call and compare it to the IMP of your own class's method. But then, it's possible that someone might swizzle an is-a on you, or otherwise play their own games, and in the end it's usually best to just say "Please don't do that."

share|improve this answer
+1, The only possible correct answer. It is up to you to indicate you don't want something overriden. And it is up to the client of your code to suffer the consequences of ignoring your warnings. –  K.Steff Jan 15 '13 at 18:04
From Why doesn't objective c support private methods - one comment starts out "Objective-C is (in some ways) a "big-boy" language in which programmers are expected to exhibit a certain amount of discipline, rather than getting slapped on the hand by the compiler/runtime at every turn." –  MichaelT Jan 15 '13 at 18:06
@MichaelT: That's an interesting sentiment, but I still find things like private and sealed in the C# compiler to be very useful as first-class constructs of the language. Real programmers write everything in assembler anyway, with all the safeties off. –  Robert Harvey Jan 15 '13 at 18:08
@RobertHarvey I absolutely agree. I find sealed jars and final classes or methods to be a very useful construct in enforcing the developer's ideal. Other languages are more code by convention and "don't stick your fingers here or things can go wrong" without having any way to enforce it. Its a trade off. –  MichaelT Jan 15 '13 at 18:14
add comment

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