Reputation: 21818
Please consider the following code:
`@interface Parent : NSObject
- (void)whoAmI;
@end
@implementation Parent
- (void)whoAmI
{
NSLog(@"PARENT CALLED");
}
@end
@interface Child : Parent
- (void)test;
@end
@implementation Child
- (void)whoAmI
{
NSLog(@"CHILD CALLED");
}
- (void)test
{
NSLog(@"%@", [super class]);// CHILD!!!! why???
[super performSelector:@selector(whoAmI)];// "CHILD CALLED" why???
}
@end
`
When i call test
method i expect to see parent class printed and parent whoAmI
method executed. But surprisingly both times the derived class is called. Can anyone explain why it happens and how do i performSelector:
on base class?
Upvotes: 1
Views: 367
Reputation: 7510
The super
method is simply a way of forwarding a message to a superclass' implementation code. However, self
remains the same. In fact, if you create an instance of Child
, there is no instance of Parent
at all. You can test this by NSLog-ing self
as a %p
to inspect the pointer address; when a super method is called, the self
pointer is the same as it was for the instance of the subclass which called it:
Parent:
- (void)printAddr {
NSLog(@"%p", self);
}
Child:
- (void)printAddr {
NSLog(@"sub: %p, self");
[super printAddr];
}
You will see that the pointers are the same if you call [aChild printAddr];
.
Now let's translate this into addressing your specific questions. First off, look at the performSelector: method. Its default implementation is in NSObject, and this implementation most likely uses self
to call the selector. Because of this, even though the method implementation is that of NSObject, the method will still be called on the real object, your subclass. If it weren't for this behavior, performSelector: would always try to call the method as if it were implemented directly on NSObject unless you implemented your own performSelector: on a subclass; obviously, this is the wrong behavior.
In addition, the same thing holds true for the -class
method. Its default implementation resides on NSObject, and obviously it would be boring id it always returned [NSObject class]
, so instead it effectively uses self
to get the class of the real object.
You can also test what I've said here by making a method on the superclass which calls another method on self
. Even if you use super
to call the first method, the second method will still be called on your subclass since self
still points to the subclass:
Parent:
- (void)method {
NSLog(@"Parent: method");
[self method1];
}
- (void)method1 {
NSLog(@"Parent method1");
}
Child:
- (void)method {
[super method];
}
- (void)method1 {
NSLog(@"Child: method1");
}
In this case, [aChild method]
will output:
Parent: method
Child: method1
Upvotes: 5