Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.objective-c > #230
| From | "Pascal J. Bourguignon" <pjb@informatimago.com> |
|---|---|
| Newsgroups | comp.lang.objective-c |
| Subject | Re: A question on designated initializers |
| Date | 2015-11-25 02:00 +0100 |
| Organization | Informatimago |
| Message-ID | <87k2p6vqoa.fsf@kuiper.lan.informatimago.com> (permalink) |
| References | <n2tcmc$t64$1@news.albasani.net> <n2u08d$7vs$1@dont-email.me> <n30iuo$c4b$1@news.albasani.net> <87wpt8ujzb.fsf@kuiper.lan.informatimago.com> <n3304v$m0n$1@news.albasani.net> |
Jon Rossen <jonr17@comcast.net> writes:
> On 11/23/2015 7:58 PM, Pascal J. Bourguignon wrote:
>> Jon Rossen <jonr17@comcast.net> writes:
>>> Last one, slightly unrelated to 2a and 2b but still about
>>> relationships between classes and sub-classes:
>>> c) At each step down the hierarchy through sub-classes, the 'plain'
>>> init would have to be overridden and it would have to return the
>>> current class' designated initializer, correct? In other words, you'd
>>> have to re-write init for each sub-class where there is a new
>>> designated initializer.
>>
>> No. If you don't change the designated initializer, then the superclass
>> -init that sends the designated initializer message will invoke the
>> designated initializer method of the current class. You only need to
>> write a new -init if it has to do something different to ensure the
>> class invariant.
>
> I'm not sure whether I wasn't clear in how I posed the question and
> you are misunderstanding me, or I'm not understanding your answer. I
> asked if you had to rewrite init where there is a new designated
> initializer, meaning that you *HAD* to re-write init. Conversely,
> when I posed the question, my assumption was that if there was NOT a
> new designated initializer then init would not have to be
> rewritten...which is what you seem to be saying here. So, I'm not
> sure why you started your answer with a 'No': It either could be that
> I wasn't clear in my question OR you did understand the question and
> I'm not understanding a fine point you are making.
Yes, I may have misunderstood.
Indeed, there are two cases:
- the new class defines a new designated initializer
(then it must override all the other initializers so they all call the
new designated initializer),
- the new class doesn't define a new designated initializer
(then it must only override the designated initializer, and can rely
on the superclasses to forward the other initializers to the
designated initializer).
However, in the later case, you might still want to override the other
initializers, so that you may call the designated initializer with
default values specific to the new class.
>> [[ColoredRectangle alloc]init]
>> calls -[Rectangle init]
>> which sends -initWithWidht:andHeight: to self
>> which calls -[ColoredRectangle initWithWidht:andHeight:]
>> which sends -initWithWidht:andHeight: to super
>> which calls -[Rectangle initWithWidht:andHeight:]
>> which sends -init to super
>> which calls -[NSObject init]
>>
> This makes sense, at least the way the method calls work with each
> other. Did you show this *just* to show the chain of events? Or, did
> you mean it to represent a workflow, in how these methods would be
> used? That's the part that doesn't make sense (using init). You are
> starting off by calling init, (which calls Rectangle's init) and sets
> of this chain of events. However, you've lost your opportunity to
> directly use the designated initializer and pass arguments to it.
> Wouldn't you just use the designated initializer in the sub-class
> (ColoredRectangle) just as it would be used in the parent class
> (Rectangle)?
Of course. If you want to make a new colored rectangle with a different
initial size, you will call the designated initializer.
[[ColoredRectangle alloc]initWithWidht:42 andHeight:33]
which sends -initWithWidht:andHeight: to self
which calls -[ColoredRectangle initWithWidth:andHeight:]
which sends -initWithWidth:andHeight: to super
which calls -[Rectangle initWithWidth:andHeight:]
which sends -init to super
which calls -[NSObject init]
(But in actual code, you would probably make a new designated
initializer for a colored rectangle:
@implementation ColorRectangle
// new designated initializer:
-(id)initWithWidth:(int)w andHeight:(int)h andColor:(int)c {
if((self=[super initWithWidht:w andHeight:h])){
color=c;
}
return self;
}
//inherited initializers:
-(id)initWithWidth:(int)w andHeight:(int)h {
return [self initWithWidth:w andHeight:h andColor:red];
}
// for init, you can rely on -[Rectangle init]
// to call -initWithWidth:andHeight:.
@end
)
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
Back to comp.lang.objective-c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 13:39 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 14:48 -0800
Re: A question on designated initializers Louis Wu <louiswu@ringworld.net> - 2015-11-22 16:26 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 17:24 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:39 +0100
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 01:18 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 16:49 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:02 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-27 17:19 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-28 13:22 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-28 17:37 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-29 03:45 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-22 19:15 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-23 18:44 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-24 04:58 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 16:42 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 02:00 +0100
Re: A question on designated initializers Greg Parker <gparker@apple.com> - 2015-11-25 01:25 -0800
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 07:38 -0800
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 15:54 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 01:49 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:51 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 04:41 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 00:25 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 14:16 +0100
Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 17:30 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-26 03:03 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-25 09:08 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 18:41 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-12-02 14:37 -0800
Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-12-03 00:58 +0100
Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:29 -0800
csiph-web