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

4

12

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?

Timothy answered 19/5, 2010 at 9:32 Comment(4)
What do you mean by security? How does java.lang.String being final improve security?Bedwarmer
@jeremyP: google "using final in java" you will get an answer.Timothy
Tried it, none the wiser. What do you mean by security. When I hear the word "security" in a programming context, I usually think of things like preventing unauthorised access to data and passwords and encryption such like. Making the String class final does not help in that respect. If you mean "robustness" then fair enough, Objective-C has immutable objects but it's up to the implementor to ensure that they stay unmutated.Bedwarmer
Perhaps he means that allowing third parties to modify the behavior of core classes like System or String will let them 'get into the system' and perform malicious actions. On non-jailbroken iOS, at least, I don't think code by a third party other than the developer of the application can be loaded/executed at runtime, but there might be other scenarios I haven't thought about...Shoeshine
C
11

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.

Chee answered 19/5, 2010 at 9:38 Comment(2)
Are you sure that we don't have any such facilities in obj-c? you meant that obj-c doesn't provide this kind of security?Timothy
Objective-c is fundamentally open - i can do anything at runtime, add classes, remove properties, swap method implementations, even dynamically compile new code on the fly. I can do that to any Classes, any properties, even private iVars, and any methods. Now, sure, you can use all these techniques to add security measures to your code (it definitely wouldn't be more efficient). But the same measures will always be available to anyone who wants to get around your techniques - so it isn't time well spent.Chee
E
8

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.

Embitter answered 19/5, 2010 at 10:14 Comment(0)
H
4

There is no final equivalent in objective-c. There are same patterns that might be good alternative, they'll give you better separation, but neither performance nor security:

  1. If you only want to allow certain methods to be overwritten using delegate pattern might be a better choice.
  2. If you do not want subclassing at all, then you can use the abstract factory pattern. Declare a public interface + factory methods, and hide the concrete implementation classes.
Historied answered 19/5, 2010 at 10:12 Comment(0)
A
1

It seems that around 2019 objc_direct attributes are available, which can be used for final methods, and remove runtime limitations

You can read more about these attributes at NSHipster, or in the original Twitter thread

@interface Base: NSObject
- (void)cannotBeOverridden  __attribute__((objc_direct)); // final
@end

@implementation Base
- (void)cannotBeOverridden { }
@end

@interface Child: Base @end

@implementation Child
- (void)cannotBeOverridden { } // Error here
@end

Usage example

Almondeyed answered 15/8, 2022 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.