Why does the call to _something from foo call B’s implementation of _something?
@interface A
@end
@implementation A
- (void) _something {
NSLog(@"A");
}
- (void) foo {
[self _something];
}
@end
@interface B : A
@end
@implementation B
- (void) _something {
NSLog(@"B");
}
@end
It only calls
B‘s implementation if it is called for object ofBclass.This is because in objective-C, all methods are “virtual” (although this is a considerable simplification), basically due to its dynamic dispatch.
To be more correct, Objective-C has no methods. Instead, you send “messages” to objects. It is handled in runtime and object itself decides how to “respond” to this message. By default, it looks up the selector in its class and calls the associated “method” (which in case of
_somethingis different for A and B).