Question about initializers in Gold Challenge


#1

Resolving the Gold challenge of this chapter I found something that I don´t undertand:

When you create a class (BNRContainer) with a designed initializer called different from the designed initializer of its superclass (BNRItem) and at the same time this last one with a designed initializer different from the designed initializer of its superclass (NSObject), do I have to override all the designed initializer of the hierarchy chain (I mean, (id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber from BNRItem and init from NSObject ) or just the designed initializer of its superclass (BNRItem in this case)? Why?

Thanks.


#2

I’d say no, you don’t have to override every designated initializer from the superclass chain, only your class’ direct super. However, this challenge seems weird to me. In real life, the container would never be a subclass of the item, that’s just weird; my question is, do you absolutely HAVE to have your designated initializer point to your super’s designated initializer?

BNRItem’s designated initializer asks for valueInDollars which is a useless parameter for the container as the value in dollars for the container is simply the aggregate sum of it’s component’s values.

So can we instead have BNRContainer’s designated initializer do the following?

The super’s init would call it’s own designated initializer and pass 0 as the valueInDollars.

I said earlier that I disagreed with having BNRContainer subclass BNRItem. A lot of the stuff in Item is useless for the container and just bloats it. A better way of doing this would be to have an abstract class BNRThing (an abstract class is like any other class, but it itself is never used; instead, it contains a bunch of generic attributes and methods (name, dateCreated, serialNumber… as well as accessors)), which would be subclassed by a lot of other classes that need it’s attributes and methods, including BNRContainer and BNRItem.

My two cents,

Ivan.

Edit: It seems my Comp Sci theory is a bit rusty, what I called an “Abstract Class” is actually called and Interface. Abstract classes are something similar but slightly different.
For the curious:
en.wikipedia.org/wiki/Class_(com … rogramming#Abstract_and_Concrete
en.wikipedia.org/wiki/Interface_ … er_science#Software_interfaces_in_object_oriented_languages


#3

Jovani; I agree, this assignment makes no sense. It is teaching bad practice. Inheritance is supposed to be used to refine an existing object, not to force an object into something that it is not.

A better assignment would have been inheriting from NSArray or NSMutableArray. That way we would be refining those classes to handle just BNRItem objects, and allow their description to properly convey those types of objects in their response, as well as adding the additional functionality of computing the total value based upon the items contained within.

I’m actually thinking of ignoring the Gold Challenge as defined and doing this instead, as the current project just feels wrong to me.

In answer to the initializer question, yes, you need to have your subclass’ designated initializer point to the super’s designated initializer. If you don’t, the parent’s variables values are “undefined” which is bad practice. That said; those variables are being used for absolutely nothing, so while it’s necessary to make sure the object in consistent, it still is a waste of time.