If I have a super class with a convenience constructor as follows (using ARC):
+(id)classA {
ClassA *foo = [[ClassA alloc] init];
return foo;
}
If I then subclass ClassA, with a class named ClassB, and I want to override the convenience constructor, is the following correct:
+(id)classB {
ClassB *foo = [ClassA classA];
return foo;
}
(Assume that I cannot call alloc and init on ClassB).
Thanks!
No, that is not correct, since that allocates, inits and returns a ClassA, not a ClassB. The only way to do this is not to use ClassA explicitly:
Of course, you could also use old-fashioned
newfor this:FWIW, assuming I would want to do more than just allocate and init, and my class is named Gadget, then I would do something like:
Of course that assumes that there is an
initWithNumber:method in my class.