1

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
Community
  • 1
  • 1
Tobias
  • 4,397
  • 3
  • 26
  • 33

3 Answers3

3

"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"

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
0

The answer is in your question:

"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."

If you don't need to do any extra initialization, there's no need to override (or create your own) designated initializer. The method lookup for initWithX:Y: will simply use the superclass implementation.

Nathan Eror
  • 12,418
  • 2
  • 29
  • 19
0

You don't need a designated initializer if your new class does not need any extra initialization. However, you might want to do so anyway, in case you decide to change your subclass later.

Moshe
  • 57,511
  • 78
  • 272
  • 425