1

I'm confused as to why my instance variables aren't working in my subclass even though the instance variables are declared in the interface file of the parent class. My subclass inherits a method from the parent class to define the instance variables. Then the subclass uses one of its own methods to display the values of instance variables but the values are zero? Why is that?

/interface file of parent class **/
#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{
    int w;
    int h;
    int j;
    int k;
    int a;
    int b;
}

 @property int width, height;


 -(void) define;


@end

/**implementation file of parent class **/   
#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

-(void)define{

    a = width;
    b = height;

}


@end

/**interface file of subclass **/
#import "Rectangle.h"

@interface Rectangle2 : Rectangle

@property int width, height;

-(void) show;

@end

/**implementation file of subclass **/

#import "Rectangle2.h"

@implementation Rectangle2

@synthesize width, height;


-(void) show{
    NSLog(@"the width and height are %i and %i", width, height);
    NSLog(@" a and b are %i and %i", a, b);
}



@end

/**Main**/
#import "Rectangle.h"
#import "Rectangle2.h"

int main (int argc, const char * argv[])
{
    @autoreleasepool {

        Rectangle * shape1;
        shape1 = [[Rectangle alloc] init];

        Rectangle2 * shape2;
        shape2 = [[Rectangle2 alloc] init];


        shape1.width =10;
        shape1.height = 5;


        shape2.width =2;
        shape2.height = 3;

        [shape2 define];
        [shape2 show];



    }

    return(0);

}

My program displays the following:

Rectangle6[900:303] the width and height are 2 and 3

2013-07-15 20:09:35.625 Rectangle6[900:303] a and b are 0 and 0

Why are the a and b 0? Since these instance variables were declared in the inheritance file of the parent class, shouldn't I be able to use them in the subclass? I'm not getting any errors so I know that we are accessing the instance variables, but why am I not displaying the right values when running?

Brosef
  • 2,945
  • 6
  • 34
  • 69

1 Answers1

1

You should call your base class methods and variables using derived class object

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{
int w;
int h;
int j;
int k;
int a;
int b;
}

@property int width, height;
-(void) define;
@end

#import "Rectangle.h"

@implementation Rectangle
@synthesize width, height;

-(id)init
{
    if (self=[super init]) {

    }
    return self;
}

-(void)define{
    a = width;
    b = height;
}

@end

Rectangle 2

#import <Foundation/Foundation.h>
#import "Rectangle.h"

@interface Rectangle2 : Rectangle
@property int width, height;
-(void) show;
@end


#import "Rectangle2.h"

@implementation Rectangle2
@synthesize width, height;

-(id)init
{
    if (self=[super init]) {

    }
    return self;
}

-(void) show
{
    [super setHeight:10];
    [super setWidth:5];

    [super define];

    NSLog(@"the width and height are %i and %i", width, height);
    NSLog(@" a and b are %i and %i",a, b);
}

@end

Main Class

int main(int argc, char *argv[])
{
    @autoreleasepool {

        Rectangle2 * shape2;
        shape2 = [[Rectangle2 alloc] init];

        shape2.width =2;
        shape2.height = 3;

        [shape2 show];


        return(0);
    }
}

output is :-------

2013-07-16 09:26:49.275 rec[894:11303] the width and height are 2 and 3 2013-07-16 09:26:49.277 rec[894:11303] a and b are 5 and 10

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
  • @Hercules I'm still new to objective-c so i'm not sure I follow what you're trying to prove. What does the init method of the code do -(id)init { if (self=[super init]) { } return self; } does the program check to see if the current object is equal to the result of init method of the superclass then return self? Why is there nothing in between the brackets of the if statement? – Brosef Jul 16 '13 at 05:23
  • @somid http://stackoverflow.com/questions/4359617/overriding-init-in-subclass may or may not shed some light on your question – rezand Jul 16 '13 at 06:16
  • @somid as u init derived class in main with the init of that base class also initialized using [super init] call in init method of derived class – Prince Kumar Sharma Jul 16 '13 at 06:19
  • Thanks for trying to explain this, but I'm having a difficult time following. I think it would help to understand why the code I provided didn't work. Thanks for help so far @Herçules – Brosef Jul 16 '13 at 07:35