2

Ok, this is going to be a really embarrassing question, but I looked like mad in my beginner's books (especially those usually helpful indices) and behold, I did not find a down-to-earth explanation of what 'super' does in my applications.

I had a look at this here, but I'm afraid that's well beyond me. Can anyone offer a very simply explanation of what 'super' does in my apps and why I should become friends with it? E.g.:

- (void)viewDidLoad {
[super viewDidLoad];}

Thanks!!

Community
  • 1
  • 1
n.evermind
  • 11,944
  • 19
  • 78
  • 122

2 Answers2

6

super calls the superclass's implementation of the method. So if your class inherits UIViewController, then [super viewDidLoad]; will call the UITableViewController class's viewDidLoad method. You should usually do this because the super class may do important things that need to happen. In the case of viewDidLoad, I'm not sure it actually does anything currently, but it always could in a future version of the framework.

GendoIkari
  • 11,734
  • 6
  • 62
  • 104
  • 1
    Many languages require that you make super calls first thing in any overridden method, this is good style regardless of requirements. – JERiv Mar 11 '11 at 21:24
  • Although with methods like `viewDidUnload` and `dealloc`, super should be the last thing called, not the first, right? – GendoIkari Mar 11 '11 at 21:26
  • @JERiv: I disagree. The point of inheritance and method override is usually to change what a method does. You should only call the super implementation if you *know* that it is a requirement, or you *know* what it will do. – e.James Mar 11 '11 at 21:26
  • 3
    I would say kinda the opposite... only don't call a super implementation if you know what it does, and know that you don't want that to happen. Otherwise, you never know if the super implementation is doing something that's required. – GendoIkari Mar 11 '11 at 21:27
  • @Gedolkari: Yes. `dealloc` is a perfect example. The Cocoa API *requires* you to call `[super dealloc]` at the end of your custom `dealloc` method. – e.James Mar 11 '11 at 21:28
  • Oh wow... this is a really controversial and philosophical question then. So the take-home-message for the beginner is: always use it, because it might do something one day. With dealloc it's necessary, so that you won't be wasting memory. Thanks! – n.evermind Mar 11 '11 at 21:34
  • I am open to the possibility that I am wrong about "good style" in this case, but in my experience, however, I am an experienced Cocoa developer. I did a quick search through my current Xcode projects, and there are only a handful of cases (out of about 600 classes) where I call the super implementation of an overridden method other than `init` and `dealloc`. – e.James Mar 11 '11 at 21:38
  • @n.evermind: You definitely need to use it for `dealloc` to avoid memory leaks, or worse. On that point, all of us agree `:)` In other cases, I strongly suggest that you check the API documentation to determine the proper course of action. Personally, I do not think that *always* calling super is a good style choice. – e.James Mar 11 '11 at 21:43
  • More discussion: http://stackoverflow.com/questions/824695/do-i-always-have-to-call-super-viewdidload-in-the-viewdidload-method – e.James Mar 11 '11 at 21:47
  • @n.evermind: the "take-home_message" isn't that you should always call it, it depends on why you are overriding - if you are changing behaviour or blocking a method then you may not wish to call super. @Gendolkari: in the original answer to be precise you are not calling the superclasses implementation but the implementation that would be called if your superclass made the call... I.e. if your superclass inherits an implementation that is the one that is called. – CRD Mar 11 '11 at 22:29
2

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).

justin
  • 104,054
  • 14
  • 179
  • 226