Reputation: 2593
just had a noob question. I'm trying to understand the difference between calling self and super. Now I understand inheritance and other fundamental OOP concepts, but the idea of self and super is still not clear to me. I'll illustrate my question with an example.
So the the below code performs a segue when the phone is tilted upside-down. I understand that "Scene2ViewController" is a subclass of "UIViewController" and so "Scene2ViewController" inherits all of UIViewController's methods. And so below I'm calling the method performSegueWithIdentifier with the receiver of the message being self. Now when I change "self" to "super" the code still executes the same way. Isn't calling super the same as calling self? If someone could explain this to me it would be appreciated, thanks.
//Scene2ViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self performSegueWithIdentifier:@"SegueToScene1" sender:self];
}
return (interfaceOrientation ==
UIInterfaceOrientationPortrait);
}
Upvotes: 2
Views: 2517
Reputation: 125037
self
and super
actually both point to the same object. super
is a keyword that tells the compiler to generate instructions that start the search for a method definition in the super class rather than in the current class.
@interface A : NSObject {}
- (void)foo;
@end
@implementation A
- (void)foo {
NSLog(@"A's foo!");
}
@end
@interface B : A
@end
@implementation B
- (void)foo {
NSLog(@"B's foo!");
}
@end
//...somewhere in a method of class B...
[self foo]; // prints "B's foo" in the console
[super foo]; // prints "A's foo" in the console
If we assume, per the comment, that the last lines are somewhere in a method of B, then self
points to some instance of B. super
also points to that same instance of B. But when you use self
to call foo
, the search for an implementation of foo
starts with class B. When you use super
, the search for a foo
starts with B's superclass, A.
super
is especially handy when you want to preserve the inherited behavior, but add something on. So, we could have B's implementation of foo
call A's version using [super foo]
. Without super
there'd be no way to call the inherited method, and calling foo
from the overridden method would result in infinite recursion.
Upvotes: 6
Reputation: 9544
Well sometimes, in a subclass, you might override a function that was already defined in the parent class. Frequently this happens in the init function. So if you need to call the parent class's init function you call super. If you need the subclass' function you call self. If only the parent has the function declared then Self and super act the same. But if only the subclass has the declaration then you cannot call the function from super.
Upvotes: 3
Reputation: 40221
When you call a method of self
(or rather send a message to self
in Objective-C terms) the runtime will search for an implementation of that method in the inheritance hierarchy, starting with self
, going up to NSObject
. So if self
implemented that method, it will be executed. If not, the super
class will be checked and so on.
Sending the message to super
is very similar, with the exception that the runtime will start looking for an implementation in super
and skip self
.
Upvotes: 5