the super init uses the NSObject's init (when we made Square a composite object, we changed it's parent class from Rectangle to NSObject).
Square's overritten init is
-(id) init
{
return [self initWithSide:0];
}
So if we use a simple
Square *mySquare = [[Square alloc] init];
the super init call still uses the overridden init method above, and not the parent method (as the "super" makes me believe it would do). That's what I still don't get. Why is "super" not bypassing this overridden init method. (I'm glad it doesn't bypass it. I just want to understand why).
Anywho, the way I understand the rest of the methods for this composite object in the following code is:
#import "Square.h"
@implementation Square
- (id)initWithSide:(double)side
{
self = [super init];
if (self) {
rect = [[Rectangle alloc] init];
[rect setWidth:side andHeight:side];
}
return self;
}
-(id) init
{
return [self initWithSide:0];
}
-(void) setSide:(double)s
{
[rect setWidth:s andHeight:s];
}
-(double) side
{
return [rect width];
}
-(double) area
{
return [rect area];
}
-(double) perimeter
{
return [rect perimeter];
}
-(void) dealloc
{
[rect release];
[super dealloc];
}
@end
initwithside sends the side argument to the Rectangle object.
All other calculations, setters and getters, are also manipulating the Rectangle object.
Throughout it all, the Square object mySquare is just a placeholder, an empty memory address whose only function is to invoke the needed methods and indirectly manipulate the rect instance, and get its returns. All this is "hidden" and it looks like we're using a square object, but the square's methods are using a rectangle object, and the square is only there to call the methods.
Am I completely off?
Thanks!!
Is that it?