Suppose there is an object that is so initialized
- (void)doInit { NSLog(@"In BaseClass init"); } - (id)init { self = [super init]; [self doInit]; return self; }
and it has a subclass that is similarly introduced
- (void)doInit { NSLog (@"In SubClass init"); } - (id)init { self = [super init]; [self doInit]; return self; }
Now, if I create an instance of a child class, I get the following output:
In SubClass init In SubClass init
when really what i meant is
In BaseClass init In SubClass init
Is there a way to mark doInit to say that it cannot be redefined or do I need to create a unique name for all methods in the subclass?
I'm not quite sure how I have not come across this problem before, but there you go.
Edit:
I understand why this is happening, I did not expect the base class to be able to call an overridden function.
I also can't just call [super doInit]; from the Subclass method, because BaseClass still needs to call doInit to create an instance of BaseClass will still work. If I called [super doInit], I would still get a SubClass doInit call twice.
It seems like the answer is no, and I just need to uniquely name each doInit as doBaseClassInit and doSubClassInit.
objective-c
iain
source share