Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7786457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:25:13+00:00 2026-06-01T20:25:13+00:00

In a book, I saw that if a subclass is overriding a superclass’s method,

  • 0

In a book, I saw that if a subclass is overriding a superclass’s method, we may have

self = [super init];

First, is this supposed to be done in the subclass’s init method?

Second, I wonder why the call is not just

[super init];

? I mean, at the time of calling init, the memory is allocated by alloc already (I think by [Foobar alloc] where Foobar is the subclass’s name. So can’t we just call [super init] to initialize the member variables? Why do we have to get the return value of init and assign to self? I mean, before calling [super init], self should be pointing to a valid memory allocation chuck… so why assigning something to self again?

(if assigning, won’t [super init] just return self‘s existing value?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:25:13+00:00Added an answer on June 1, 2026 at 8:25 pm

    So why assign the value returned from [super init] to self? Looking at
    a typical initializer method:

     - (id)initWithString:(NSString *)aString {
         self = [super init];
         if (self)
         {
             instanceString = [aString retain];
         }
         return self; }
    

    Why do we assign [super init] to self here?

    The textbook reason is because [super init] is permitted to do one of
    three things:

    1. Return its own receiver (the self pointer doesn’t change) with
      inherited instance values initialized.
    2. Return a different object with
      inherited instance values initialized.
    3. Return nil, indicating failure.

    In the first case, the assignment has no effect on self and the
    instanceString is set on in the original object (the line
    instanceString = [aString retain]; could have been the first line of
    the method and the result would be the same).

    In the third case, the initialization has failed. self is set to nil,
    no further action is taken and nil is returned.

    The rationale for assigning to self is associated with the second
    case: if the returned object is different, we want the:

    instanceString = [aString retain]; which gets converted to
    
    self->instanceString = [aString retain]; to act on the correct value,
    

    so we have to change the value of self to point to this new object.

    hoping this helps…

    From Cocoa with Love

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Saw this piece of code in a Ruby on Rails book. This first one
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast
I'm learning C and I saw in a book that a function prototype has
I'm reading a book about SQL. In that book, I saw strange query below:
The book that I purchased to help with my SSIS understanding seems to have
The book, Producing Open Source Software , suggests that it's better to have lengthy
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
I saw in Crockford's Book Javascript: The Good Parts that he does typeof comparison
I saw this Java snippet in the book Spring in Action , but I'm
I noticed, as well as saw in the Essential C# 3.0 book, that paramters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.