at the low level, self
contains a pointer to the set methods it responds to. this is a basic mechanism for the implementation of dynamic dispatch. each class is given a unique set. if you're familiar with c++, this is similar in concept to a virtual table. you can think of an objc method as being like a C function with 2 hidden arguments (self,_cmd).
super
is a dynamically created representation of self
, with a pointer to the next-in-line methods implemented by the instance's superclasses. this representation is based on self
, and just directs to another set of implemented methods.
@interface MONTypeA : NSObject
- (void)monMethod;
@end
@interface MONTypeB : MONTypeA
- (void)monMethod;
@end
@implementation MONTypeA
- (void)monMethod {
printf("i am MonTypeA\n");
}
@end
@implementation MONTypeB
- (void)monMethod {
[super monMethod]; /* << will call -[MONTypeA monMethod], which will print "i am MonTypeA\n" */
printf("i am MonTypeB\n");
}
@end
if you create an instance of MONTypeA
, then it will respond to monMethod
:
MONTypeA * a = [MONTypeA new];
[a monMethod];
[a release], a = 0;
// outputs: "i am MonTypeA\n"
MONTypeB * b = [MONTypeB new];
[b monMethod];
[b release], b = 0;
// outputs: "i am MonTypeA\n""i am MonTypeB\n" because -[MONTypeB monMethod] calls through super
therefore, calling super
performs the implementation of the method of the superclass in this specific case.
it is important to remember: the set of methods super
refers to is always those of the previous implementation of the method in the hierarchy, it is not the same set as the set of instance methods which an instance of the superclass would be given (unless your class were to override every method).