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 9012685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:04:03+00:00 2026-06-16T03:04:03+00:00

I have a subclass SubClass that inherits from baseclass BaseClass. BaseClass has an initializer,

  • 0

I have a subclass SubClass that inherits from baseclass BaseClass.

BaseClass has an initializer, like so:

-(id)init {
    self = [super init];
    if(self) {
       [self commonInit];
    }
  return self;
 }

 -(void)commonInit {
     self.goodStuff = [[NSMutableArray alloc]init];
 }

SubClass does its initializer, like so:

-(id)init {
    self = [super init];
    if(self) {
       [self commonInit];
    }
  return self;
 }

 -(void)commonInit {
     self.extraGoodStuff = [[NSMutableArray alloc]init];
 }

Now, I’ve *never taken a proper Objective-C course, but I’m a programmer more from the Electrical Engineering side, so I make do. I’ve developed server-side applications mostly in Java though, so I may be seeing the OO world through Java principles.

When SubClass is initialized, it calls the BaseClass init and my expectation would be — because inheritance to me implies that characteristics of a BaseClass pass through to SubClass — that the commonInit method in BaseClass would be called during BaseClass init.

It is not. I can *sorta understand maybe-possibly-stretch-my-imagination why it wouldn’t be. But, then — why wouldn’t it be based on the principles of OOP? What does “self” represent if not the instance of the class of the running code?

Okay, so — I’m not going to argue that what a well-developed edition of Objective-C is doing is wrong. So, then — what is the pattern I should be using in this case? I want SubClass to have two main bits — the goodStuff that BaseClass has as well as the extraGoodStuff that it deserves as well.

Clearly, I’ve been using the wrong pattern in this type of situation. Am I meant to expose commonInit (which makes me wonder about encapsulation principles — why expose something that, in the Java world at least, would be considered “protected” and something that should only ever be called once for each instance)?

I’ve run into a similar problem in the recent past and tried to muddle through it, but now — I’m really wondering if I’ve got my principles and concepts all straight in my head.

Little help, please.


Let me clarify — I get that self ends up being SubClass when I call init on super. That I can see when I debug, etc.

What’s the pattern for overriding methods in this sort of situation? Where I have a bit of common initialization that may get called from several init methods in the super class? Do I have to put the code in every variation of init?

  • 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-16T03:04:08+00:00Added an answer on June 16, 2026 at 3:04 am

    self in BaseClass’ constructor in your example is of type SubClass, so [self commonInit] calls SubClass‘ commonInit override , not BaseClass‘ commonInit method.

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

Sidebar

Related Questions

I have subclass that inherits from a dict. On __getitem__ method I'd like to
I have a class that inherits from UIView, and this class has some controls
I'd like to have a BaseViewController class (that inherits from UIViewController ) for my
I have a subclass that calls a method from a superclass. The method in
I want to have a UIView subclass that has a border image, but I
I have a custom NSWindowController subclass that loads a NIB file during initialization like
Is it possible to subclass and inherit from javascript Arrays? I'd like to have
I have a subclass of NSObject that implements an -(id)initWithRootElement:(MyElement *)e method. NSXMLDocument has
Using JavaScript how do I create a subclass that inherits values from the parent
I have a base class, a middle class that inherits from the base, and

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.