What does [super init] returns exactly?


I have some issues understanding the part where we write the init method of the LotteryEntry.

Here’s the code:

- (id)init
    self = [super init];
    if (self) {
        firstNumber = ((int)random() % 100) + 1;
        secondNumber = ((int)random() % 100) + 1;        
    return self;

What I don’t get is the

By running [super init], don’t we get a pointer to the superclass’ object? So in that case, to a NSObject?
If not, how does it work? How is it possible to call [super init] and get back a pointer to an object of the current class?

I know it’s pretty straightforward to write anyway, but I’m wondering. Obviously after some time of reflection, if [super init] returns a pointer to an object of the current class, all the rest makes sense. But I’m still blocking on the way this is assigned.



The return value from [super init] is a pointer to the current object/self – it doesn’t return a pointer to the superclass as there really isn’t any such thing. Super can only be used to call the superclass’s implementation of the current method (it isn’t an ordinary pointer).

We assign self = [super init] here because in some special cases an initializer will return a different object than what was returned by +alloc initially.



Hi Adam.
Thanks for the answer. I kind of understand all of that, it’s just the way it is written that seems awkward to me. But as you said it’s not an ordinary pointer, so I guess that’s just the answer I need.
Thanks again.

EDIT: actually I think I got it, thanks to your reference to alloc. First we alloc, which gives us an address. Then we init, and in our init we call the superclass’ init. And if something went wrong or if the superclass found an optimized way, it’s going to return an address that is different from the one given by alloc. So we assign and test.
That’s how I get it now, make sense to me.