Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid superclass methods getting overridden by sub class in objective - c

Like in java:

A final class cannot be subclassed. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.

How can I achieve this behavior in objective-c?

like image 872
Manjunath Avatar asked May 19 '10 09:05

Manjunath


2 Answers

You can't. Efficiency doesn't come into it. If you are that bothered about security don't use objective-c. There will always be a way to get around any measures you take.

like image 149
hooleyhoop Avatar answered Nov 16 '22 13:11

hooleyhoop


As has been said a number of times, you can't.

However, if you are making a library (which is the only case in which I could see this being relevant, anyway) there are a few steps you can take. Well, one, really.

Write, in the documentation of the class, that "This class is not intended for subclassing." (ref. NSIndexSet) or "Do not override this method." (ref. +[NSApplication sharedApplication].

As a way of explanation, it is worth noting that (pretty much) everything that happens in Obj-C, and that separates it from C, happens at runtime, and the runtime is, so to speak "Right There". Any piece of code can inspect, mutate or pervert the runtime at their leisure, making Obj-C a terribly powerful language; especially with regards to its "meta-language" structure.

like image 8
Williham Totland Avatar answered Nov 16 '22 14:11

Williham Totland