Can anyone please guide me on how to avoid overriding of superclass methods in subclass in Objective-C, like the "final" concept in Java.
You can't. You can't even be sure that a leaf class's methods are the ones you supplied, because we can use the Objective-C runtime library to replace method implementations in a running application. That's a deliberate feature of the runtime library's design.
Well I'm not sure about a "final" equivalent in objective c but if there are methods in a super class that you don't want called just don't write that particular method in your sub class
Plus I think a method written with "+" prefix as opposed to "-" is a class method and not an instance method so that method should always be the same.
Related
I find a c++ system method causes crash in ios and I try to swizzle the method. However, I do not how to do that because it's a method of a c++ class. Anyone know whether can I do that?
Method swizzling is unique to objective-c (and even there one has to use it carefully), and is not applicable to c++.
I suppose that you don't have access to the source code of the c++ class.
Then the only way to "exchange" the implementation of a method at a specific c++-class is to derive a subclass, override the method, and then make sure that the subclass is used instead of the other class. It is still unlikely that you have a chance; the method being not virtual, the class to be replaced being used in non-polymorphic ways, the class to be replaced already having several subclasses, each of these points will prevent you from being successful.
Good luck though!
In objective-c if I have a class such as "Foo", and have a category for that class "Foo (bar)", but do not implement all the methods declared in the category, would I have to redeclare them in a subclass before I define them? My book says yes (not sure if this is a mistake, or has been changed), I don't see why this is the case.
Basically how do categories apply to subclasses?
Categories are orthogonal to class hierarchy. They apply to the class on where they are defined. At runtime, the category methods are added to the method table of the class. Subclasses can use them as if they were regular methods.
Be sure to (re-)read this chapter of Objective-C Programming Language about the subject.
If you want to override a category method in a subclass, you can to either by declaring it in the class interface, or by declaring a category for the subclass.
Hope it helps.
You don't need to redeclare the method, but you must be able to "see" the declaration if you are calling it internally. (i.e. #import 'Foo+bar.h' in your subclasses .m).
It is, however, not a good idea to declare a method but not implement it. Your application will crash if -[Foo someDeclaredButNotImplementdMethod] is called. At least provide an empty implementations (e.g. - (void)someDeclaredButNotImplementdMethod {}).
I have a class with several subclasses.
They all override a class method, but I don't have a specific implementation for the method in the superclass.
Since I can't just declare it in the interface but I need to implement it as well (to avoid debugger warnings), I was wondering if I can just provide empty implementations of the method in the superclass.
The reason why I'm adding the methods definitions to the superclass is that I've a multi-target project, the current application delegate is considered with the specific overridden method:
[(GenericDelegate *)[NSApp delegate] myMethod];
thanks
Yes, this is a perfect normal practice. In fact, it has a name: a "Template Method." You search for that in the Cocoa documentation.
You will find that Apple also does it occasionally in their own code. The drawRect: method in UIView is the first one that comes to mind.
So, anyway, yes, if it suits your needs, I would go ahead and do it. Just make sure that you think through whether or not, for example, a protocol wouldn't suit your needs better.
There are other options as well. Check out the answer/discussion over here: Does Objective-C have something like C++ virtual functions?
All methods are virtual in objective c, "pure" virtual (as in C++) function don't exist and hence the equivalent methods in objective c need an empty implementation in the superclass, just to silence the compiler warning (I don't think there is any other way to do so). There is nothing wrong with that. This post is related to your question.
Does a method which I check for with respondsToSelector have to actually exist?
What if I only define it in the interface part and fail to implement it? I'm looking at a poor-man's virtual function in Objective-C.
First, yes the method actually has to exist for the check to succeed in the context you describe. respondsToSelector: will return NO if the method is not implemented.
More importantly, I think you mean a poor man's pure virtual function in Objective-C. All instance methods are "virtual" in Objective-C; since method lookup is done a run-time, the subclass' implementation will always be used, even from a pointer of the superclass type. In Objective-C, there is no such thing as a pure virtual base class. You can often achieve what you want by either using a #protocol to define an API or using a base class that provides an implementation that throws an NSNotImplementedException as its body. Subclasses would obviously have to override the implementation, making it effectively pure virtual.
Given that calling respondsToSelector: only makes sense when you don’t know whether a method exists, it’s not entirely clear what you mean.
If you mean, does some implementation of a method with the specified selector have to exist somewhere, the answer is no. Selectors merely represent names of methods. The #selector directive doesn’t reference any aspect of any method implementation.
respondsToSelector will return NO, since the selector isn't callable at run-time. The interface part only affects compilation.
If I have an instance of class B which is a subclass of class A, is there a way for me to turn my instance of class B into an instance of class A without explicitly writing code to do it?
I do not mean simply downcasting with the standard c syntax.
You may be able to do this with the objc runtime (see the object_setClass(id object, Class cls) in the Objective-C runtime reference. The more important point, however, is that you almost certainly do not want to do this. If your subclass does not follow the Liskov Substituion Principle, it shouldn't be a subclass (i.e. an inheritance relationship is not appropriate and you should choose some other design). You can always invoke the superclass' method implementations with [super someMethod] from within your subclass.