Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting instance variables in Objective-c

In Objective-c 2.0 why do subclasses need to reference instance variables in parent classes using the self keyword?

Consider this example:

// a.h
@interface MyClass : NSObject
@property (nonatomic, retain) Object *myObject;
@end

// a.m
@implementation MyClass
@synthesize myObject;
@end


// b.h
@interface AnotherClass : MyClass
@end

// b.m
@implementation AnotherClass
- (void) someMethod {
    // error
    // Object *obj = myObject;

    // works
    // Object *obj = self.myObject;
}
@end
like image 340
SundayMonday Avatar asked Dec 14 '11 21:12

SundayMonday


People also ask

Can we inherit instance variables?

I know that instance variable are not inherited but they can be accessed in sub class. If they can be accessed in sub class then does that means that they are shared between super class and subclass or both super class and subclass have different copy.

What is inheritance in Objective C?

“ A key idea in all object-oriented programming, is inheritance that a new class can base itself on an existing class. It's another form of code reuse.

Do subclasses inherit variables?

A subclass also inherits variables and methods from its superclass's superclass, and so on up the inheritance tree.

Is multiple inheritance possible in Objective C?

In keeping with its clean and simple design, Objective C does not support multiple inheritance, though features have been developed to replace some of the functionality provided by multiple inheritance (see run-time section below). The root of the Objective C class hierarchy is the Object class.


1 Answers

You haven't actually defined a variable, you only defined a property (which implicitly defines a variable that is private). And since property are just method, you need the dot syntax. Note that self.property is the same as [self property].

To fix this, specify a variable. I'll give you an example where the variable has a different name than the property. Most people chose the same name for both but I like to have them differ so I immediately see which one is meant.

// a.h
@interface MyClass : NSObject {
    // Instance variables are "protected" by default, except if you
    // use @private or @public.
    Object *myObjectVar;
}

@property (nonatomic, retain) Object *myObject;
@end

// a.m
@implementation MyClass
@synthesize myObject = myObjectVar;
@end


// b.h
@interface AnotherClass : MyClass
@end

// b.m
@implementation AnotherClass
- (void) someMethod {
    // works
    Object *obj = myObjectVar;

    // works
    obj = self.myObject;

    // the same as self.myObject
    obj = [self myObject];
}
@end

Note the difference when you assign: if you assign to your variable the object is not retained automatically. But it is retained if you use the property:

myObjectVar = someObject; // not retained, old object not released!
self.myObject = someObject; // old object released, new object retained
[self setMyObject:someObject]; // same as the line above

Edit: Mentioned that the synthesized instance variables are private by default, as noted by @Jason Coco. And @NSGod is right that normal instance variables are protected by default rather than public, fixed that.

like image 173
DarkDust Avatar answered Sep 23 '22 03:09

DarkDust