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

  • Home
  • SEARCH
  • 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 3798892
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:38:47+00:00 2026-05-19T13:38:47+00:00

Do subclasses that don’t need to add class-specific initialization code have to implement the

  • 0

Do subclasses that don’t need to add class-specific initialization code have to implement the designated initializer of the super class?

The Apple’s documentation for NSObject’s init method provides some discussion:

"Every class must guarantee that the
init method either returns a fully
functional instance of the class or
raises an exception. Subclasses should
override the init method to add
class-specific initialization code."

"If a subclass does any initialization
of its own, it must define its own
designated initializer. This method
should begin by sending a message to
super to invoke the designated
initializer of its superclass."

However, nothing is specified for the case when a subclass needs no extra initialization code.

The code below attempts to clarify my question. ClassA has the designated initializer named initWithX:Y:. ClassB does not need to an extra initialization, everything is provided by ClassA.

Explanatory Code:

@interface ClassA : NSObject {
  NSInteger x;
  NSInteger y;
}

- (id)initWithX:(NSInteger)initX Y:(NSInteger)initY;

@end

@implementation ClassA 
    
- (id)initWithX:(NSInteger)initX Y:(NSInteger)initY {
  if(self = [super init]) {
    x = initX;
    y = initY;
  }
  return self;
}
@end

@interface ClassB : ClassA {
  //no extra variables
} 

//some extra static methods add to provide some differences 
//  between ClassA and ClassB in sample code
+ (NSInteger)extraMethodOne;
+ (NSInteger)extraMethodTwoWithInteger:(NSInteger)anInteger;

@end

@implementation ClassB

/////////////??QUESTION HERE??///////////////////
//Is the implementation below needed?
//If I call [[ClassB alloc] initWithX:1 Y:2], won't it run the code in ClassA
//  with the self set to whatever [ClassB alloc] is?
/////////////////////////////////////////////////
- (id)initWithX:(NSInteger)initX Y:(NSInteger)initY {
  if(self = [super initWithX:initX Y:initY]) {
    /* Don't need class-specific initialization code */
  }
}

//some extra static methods add to provide some differences 
//  between ClassA and ClassB in sample code
+ (int)extraMethodOne {return 1;}
+ (int)extraMethodTwoWithInteger:(NSInteger)anInteger {return 2 + anInteger;}

@end
  • 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-05-19T13:38:48+00:00Added an answer on May 19, 2026 at 1:38 pm

    “However, nothing is specified for the case when a subclass needs no extra initialization code.”

    That is correct — do nothing.

    When Apple says “”If a subclass does any initialization of its own … blah blah” what they mean is “If and only if a subclass does any initialization of its own … blah blah … otherwise do nothing at all.”

    It’s worth remembering that: it’s not very often you will build an entire massive subclass that has a whole chain of it’s own initializers (the designated initializer, and more beyond that), and then you (or others) will be subclassing that new subclass. (If it is true that you or others will be subclassing that new subclass, then, yeah, that’s when you have to have been careful to do just what Apple says above.)

    In practice 99% of the time, you are just subclassing UIView, and you put some code in the “init” framework XCode automatically pastes in for you. If, incredibly, someone then subclassed our subclass there, they would have to figure out that, wait for it, “init” is the designated initialiser in your subclass!

    Regarding the Apple doco. They do an amazing job, but, there are many examples of that flavour of problem in the doco. A good example: Is there a proper way to handle overlapping NSView siblings?. Notice the part that says “nonsensical”

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

Sidebar

Related Questions

I have an NSDictionary in which I use my own classes (NSObject subclasses) as
I have an inheritance hierarchy with overlap. The system knows about People that can
Coming to Python from Java, I've been told that factories are not Pythonic. Thus,
I have a CoreData model (managed object) called Item: @interface Item : NSManagedObject {
I have a TabbarController with different ViewControllers. Some of these ViewControllers should autorotate and
I'm writing some code in VB.Net which I hope demonstrate to colleagues (not to
My document-based Cocoa application uses a NSOutlineView/NSTreeController combo, bound to the document's Core Data
As an example, when I'm using an NSMutableDictionary, I know it inherits all the
I'm moving a project off of a custom DAL and onto Nhibernate but I've
I'm having an issue where some of my views look incorrect on App load

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.