Reputation: 57
what is this saying as stated in apples doc:
Sometimes the designated initializer of a superclass may be sufficient for the subclass, and so there is no need for the subclass to implement its own designated initializer. Other times, a class’s designated initializer may be an overridden version of its superclass's designated initializer. This is frequently the case when the subclass needs to supplement the work performed by the superclass’s designated initializer, even though the subclass does not add any instance variables of its own (or the instance variables it does add don’t require explicit initialization).
Is it saying that i don't have to create a designated initializer for the subclass and the superclass designated initializer will suffice and if so how will the subclasses properties be initialized? and in what scenario if this allowed? How would that work?
Also if your overriding the DI how can you call that method from the subclass as the parent class has the same DI as you do? What does it mean that it needs to supplement the work?
Upvotes: 0
Views: 380
Reputation: 318814
Example:
There is a superclass A
with an initWithName:
DI.
Now you create subclass B
. If you want the same DI and you don't need any additional initialization, then there is nothing to do with the init
method. You simply call:
B *someBObject = [[B alloc] initWithName:@"A Name"];
This creates the B
object and calls the initWithName:
method from A
.
Now if your B
class needs to do supplemental work in the initWithName:
method, then you add this to B.m
:
- (instancetype)initWithName:(NSString *)name {
self = [super initWithName:name];
if (self) {
// do some additional stuff to initialize this "B" instance
}
return self;
}
Upvotes: 3