Re: State of instance variables defined at instantiation?
Re: State of instance variables defined at instantiation?
- Subject: Re: State of instance variables defined at instantiation?
- From: Sherm Pendley <email@hidden>
- Date: Wed, 28 May 2003 00:47:32 -0400
On Tuesday, May 27, 2003, at 09:02 PM, Ben Dougall wrote:
i see. i was just curious. i wasn't sure if init was doing something
extra behind the scenes, because often i can't see the point of it.
One point of consistently calling -init is that it helps enforce
encapsulation - an important aspect of object-oriented programming.
If you always call -init, then you never need to worry about whether the
object you're initializing has a meaningful implementation of it. If it
doesn't, but one is added later, your code won't break. That works the
other way, too; if you add a meaningful -init method to a class that
previously lacked one, existing code that uses your class won't break,
so long as it followed the established pattern of always calling -init.
To put it another way, not every call to -init is necessarily useful in
its own right. What's useful is the design pattern that's established by
calling it, even when it doesn't strictly need to be called.
If you don't see a point in implementing your own -init, you're rarely
forced to implement one. Assuming that your class descends from
NSObject, then you only need to override the inherited -init if the
inherited method isn't sufficient for your needs. And if you're creating
a new root class, implementing a do-nothing -init method will be among
the least of your worries. ;-)
sherm--
Welcome to Rivendell, Mr. Anderson.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.