So I'm not sure where I mentioned this in the book, but the Rectangle's initWithWidth:andHeight: method should be declared to return type id not Rectangle *.

It's just for these type of cases, either where super is called (as you are doing) or an inherited method is used (through subclassing) that you want the method to return a generic object pointer (type id) as opposed to a distinct class.

In your case, the warning comes from here:

Code: (Objective-C)

self = [super initWithWidth: side andHeight: side];

since self is a Square object and the init routine returns a Rectangle.

So I'm not sure where I mentioned this in the book, but the Rectangle's initWithWidth:andHeight: method should be declared to return type id not Rectangle *.

It's just for these type of cases, either where super is called (as you are doing) or an inherited method is used (through subclassing) that you want the method to return a generic object pointer (type id) as opposed to a distinct class.

In your case, the warning comes from here:

Code: (Objective-C)

self = [super initWithWidth: side andHeight: side];

since self is a Square object and the init routine returns a Rectangle.

Hope that makes sense!

Cheers,

Steve

Now I'm really confused. Doesn't exercise 2 say to use the method added in exercise 1 which does return Rectangle, not id?

Yes, you're right. I just realized now that I don't address the issue of returning an id type for an initializer until page 354 in the text. I will have to address this earlier in a future printing/edition.

I understand the reason for return to be of type id in this case and in others where the super class returns an object to a subclass trying to set a variable of it's own type to the super's return but which way is the best and why: 1) Set the super class's method to be return type id or 2) to type cast the return object from the super class to that of the subclass?

Number 1 is better, as id is used to represent a generic type. If you declare your init routine to return an id type you're also indicating that the class might be subclassed; so that aids program readability. In either case, you should typecast the result to the appropriate object type, and that again is a readability issue.