question on init behavior

Posts: 306
Joined: 2009.03
Post: #1
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.
Quote this message in a reply
⌘-R in Chief
Posts: 1,237
Joined: 2002.05
Post: #2
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.


A.h has:

@interface A {
   ... vars;
// public methods
// no private methods

A_internal.h has:

@interface A (PrivateMethodsThisNameDoesntReallyMeanAnything)
// all of the method

A.m has:

#import "A.h"
#import "A_internal.h"

// implement public methods

@implementation (PrivateMethodsThisNameDoesntReallyMeanAnything)
// implement methods from A_internal

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.
Quote this message in a reply
Posts: 608
Joined: 2002.04
Post: #3
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.
Quote this message in a reply
Posts: 306
Joined: 2009.03
Post: #4
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  re-init a view controller...from within aerospaceman 8 4,105 Jun 30, 2009 07:24 PM
Last Post: aerospaceman
  Rare behavior. rendering<->logic sync howto godexsoft 3 2,921 Jan 2, 2009 06:00 AM
Last Post: godexsoft