question on init behavior
From what I can tell, there is nothing preventing you from skipping inits(constructors) in your object inheritance.
Example:B extends A, B has init which sets up some variables
somebody comes along and sees that A init and does
B *b=[[B alloc] initTheAOne];
instead of initTheBOne. Important stuff is missed from the b constructor and the things fails. Is there some way to hide the parent class constructors? Comming from my java background, but it seems like nobody should ever call a constructor on your super class and bypass yours.
Thoughts?
Example:B extends A, B has init which sets up some variables
somebody comes along and sees that A init and does
B *b=[[B alloc] initTheAOne];
instead of initTheBOne. Important stuff is missed from the b constructor and the things fails. Is there some way to hide the parent class constructors? Comming from my java background, but it seems like nobody should ever call a constructor on your super class and bypass yours.
Thoughts?
If A is an abstract class that shouldn't be used directly, then you can declare all of its methods in a category, rather than the main class interface, and then keep that category separate from everything else.
IOW,
A.h has:
A_internal.h has:
A.m has:
B.h imports A.h as normal. B.m would additionally import A_internal.h, and everything else would import just B.h as needed.
IOW,
A.h has:
Code:
@interface A {
... vars;
}
// public methods
// no private methods
@endA_internal.h has:
Code:
@interface A (PrivateMethodsThisNameDoesntReallyMeanAnything)
// all of the method
@endA.m has:
Code:
#import "A.h"
#import "A_internal.h"
@implementation
// implement public methods
@end
@implementation (PrivateMethodsThisNameDoesntReallyMeanAnything)
// implement methods from A_internal
@endB.h imports A.h as normal. B.m would additionally import A_internal.h, and everything else would import just B.h as needed.
Or B could just implement initTheAOne and throw an exception, log an error, or claim it was just kidding when it said it responds to initTheAOne.
Sorry I lost track of this post. I have used the category solution for private functions, but for constructors, if i put them in a category I would have to include that category I guess in anyone who wanted to instantiate it. It still wouldn't hid super class constructors if they existed outside of a catagory(say for example from a coca class that I can't modify). The throwing exceptions part isn't really an option as you would have to override every init any time you subclass and that seems like unmaintainable and kludgy. I think thats just how this language works unfortunately.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| re-init a view controller...from within | aerospaceman | 8 | 3,715 |
Jun 30, 2009 07:24 PM Last Post: aerospaceman |
|
| Rare behavior. rendering<->logic sync howto | godexsoft | 3 | 2,717 |
Jan 2, 2009 06:00 AM Last Post: godexsoft |
|

