Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:57:05+00:00 2026-05-27T11:57:05+00:00

In Objective-c 2.0 why do subclasses need to reference instance variables in parent classes

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:57:05+00:00Added an answer on May 27, 2026 at 11:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using an Objective c class, a subclass of NSObject. This class cannot
When should I use primitives in Objective-C instead of NSValue subclasses? This code is
I'm trying to implement the Strategy pattern using Core Data and Objective C. To
I have a Class (but no instance) and need to know if it conforms
I'm working with Objective-C and I need to add int's from a NSArray to
In Objective-C, what is the actual difference between putting an instance variable in .h
I'm making a class that initializes instances of certain classes. This class will be
I'm using cocos2d-x [lua binding] to port my cocos2d [objective-c] game and I had
In Objective-C, I have a category for a class: @interface UILabel(CustomInit) - (id)initWithCoder:(NSCoder *)coder;
Is is possible in Objective-c runtime to get an array of all subclasses of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.