Test Your Understanding - Differences between self and super


(This topic was previously under General Book Discussion.)

What are the differences between self and super?
Can you print out the memory address of super? Can you assign to it?

@implementation Foo

- (id)initWithBar:(Bar *)bar
    if (self = [super init])
    return self;

- (void)whereIsFoo
     NSLog (@"I am at: %p", self); // Can you do this?
     NSLog (@"My super is at: %p", super); // Can you do this?


If you are not sure, then you should read: Messages to self and super in Defining a Class in The Objective-C Programming Language in Xcode’s documentation set.


In looking through the book’s index, I note that super is not introduced until Chapter 18. Are you including the “test” of understanding just to whet our appetites and challenge our investigative skills?


Yes. And don’t leave home without it (that is, being able to tell the difference) after you have finished the book.


Both those NSLogs will give you errors.

Super is the superclass of the object - thus it has no “address”

Same applies to self I think.


Did you try?


Yes I did and got errors on both lines. However having read the book some more it seems the second line is indeed fine when referring to the object calling the method.