Amazon.com Widgets confused by [super init] on 11.5
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 02, 2014, 02:07:31 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 11
| | | |-+ confused by [super init] on 11.5
Pages: [1] Go Down
Print
Author Topic: confused by [super init] on 11.5 (Read 865 times)
xjames
Newbie
*
Posts: 26






on: September 13, 2011, 10:21:20 AM

in the following code for the square class implementation file:
Code: (Objective-C)
- (id)initWithSide:(double)side
{
    self = [super init];
    if (self) {
        rect = [[Rectangle alloc] init];
        [rect setWidth:side andHeight:side];
    }
   
    return self;

wouldn't super init use the init method from square's parent class, NSObject, thus bypassing the overridden init method for the square class?

also, when we return self, what are we returning, since the argument is going to the rect variable? Aren't we just left with a proper rectangle object, and an "empty" square object?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: September 13, 2011, 01:12:15 PM

The init method used with the call to super  would be the Rectangle 's init  method, since that's the parent class of the Square  class.   If the Rectangle 's init  method has not been overridden, then that will be the init  method inherited by the Rectangle  class, or NSObject 's init  method.

When you return self , you're likely returning the same object that was sent the initWithSide:  message (the Square  object you allocated).  However, the init  method does have the "right" to change that object (e.g, move it someplace else, create a new object, etc.), which is why the init  call to super  takes the returned value, assigns it to self,  and then the initWithSide:  message returns that value--it could have changed.

Cheers,

Steve
Logged
xjames
Newbie
*
Posts: 26






Reply #2 on: September 13, 2011, 05:39:02 PM

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
Code: (Objective-C)
-(id) init
{
    return [self initWithSide:0];
}

So if we use a simple
Code: (Objective-C)
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:
Code: (Objective-C)
#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?
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.