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

  • SEARCH
  • Home
  • 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 6915923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:32:11+00:00 2026-05-27T09:32:11+00:00

I have a doubt regarding property redeclaration Overview: class A is the parent class

  • 0

I have a doubt regarding property redeclaration

Overview:

  • class “A” is the parent class with a readonly property int n1;
  • class “B” is the subclass, which redeclares the property as read write
  • using the setter of class “B” the property value is set as 20
  • when i print the value using the getter and the instance variable I seem to get different values

Points to note: – Memory management = ARC (Automatic Reference Counting)

Question:

  • When I print the values of self.n1 and _n1 why do I get different values ?
  • My expected behavior and actual behavior don’t match why (Pls scroll down to see the actual vs expected) ?

Code: (in separate files)

A.h

#import<Foundation/Foundation.h>

@interface A : NSObject

@property (readonly) int n1;

- (void) display;

@end

A.m

#import "A.h"

@implementation A

@synthesize n1 = _n1;

- (void) display
{
    printf("_n1     = %i\n", _n1);                  //I expected _n1 and self.n1 to display the same value
    printf("self.n1 = %i\n\n", self.n1);            //but they seem to display different values
}

@end

B.h

#import"A.h"

@interface B : A

@property (readwrite) int n1;

@end

B.m

#import"B.h"

@implementation B

@synthesize n1 = _n1;

@end

test.m

#import"B.h"

int main()
{
    system("clear");

    B* b1 = [[B alloc] init];

    b1.n1 = 20;

    [b1 display];   //Doubt - my expected behavior is different from actual behavior


    return(0);
}

Expected Behavior:

_n1     = 20
self.n1 = 20

Actual Behavior:

_n1     = 0
self.n1 = 20
  • 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-27T09:32:12+00:00Added an answer on May 27, 2026 at 9:32 am

    There are two ways of going about this. In neither case do you call @synthesize in the subclass. I’m surprised that compiles for you. I would expect an error like “Property ‘n1’ attempting to use ivar ‘_n1’ declared in superclass ‘A'”. In any case it’s definitely not something you can really do, which is why you’re seeing strange behavior. (I remembered why you aren’t seeing this error; it’s because of the separate compile units. You’re just winding up with different ivars.)

    First, you need to understand @dyanmic. This is a way of telling the compiler “yes, I know you don’t see an implementation for the required method here; I promise it’ll be there at runtime.” In the subclass, you will use @dynamic to let the compiler know that it’s ok to inherit n1.

    @implementation B
    @dynamic n1;
    @end
    

    Now, you need to provide the setN1: method. IMO, subclasses shouldn’t go messing with their superclass’s ivars, so I approve of the fact that synthesized ivars are marked @private. In a second, I’ll tell you how to undo that, but for now let’s deal with my preferred solution:

    • Implement setN1: as a private method in A.
    • Expose it in B.

    A.h

    @interface A : NSObject
    @property (readonly) int n1;
    - (void) display;
    @end
    

    A.m

    #import "A.h"
    
    @interface A () // Private class extension, causes setN1: to be created but not exposed.
    @property (readwrite) int n1;
    @end
    @implementation A
    
    @synthesize n1 = _n1;
    
    - (void) display {
       ...
    }
    @end
    

    B.h

    #import "A.h"    
    @interface B : A
    @property (readwrite) int n1; // Tell the world about setN1:
    @end
    

    B.m

    #import "B.h"
    @implementation B
    @dynamic n1; // Yes compiler, setN1: exists. I promise.
    @end
    

    Now, some people think it’s fine for subclasses to mess with their superclass’s ivars. Those people are wrong (ok, IMHO…) but it is possible in ObjC. You just need to declare the ivar @protected. This is the default when you declare ivars directly in the @interface (one of many reasons you shouldn’t do this anymore). It would look like this:

    A.h

    @interface A : NSObject {
      int _n1;
    }
    ...
    

    A.m — remove the extra class extension that makes n1 writable in the superclass.

    B.h — no change

    B.m

    @implementation B
    @dynamic n1;
    
    - (void)setN1:(int)n1 {
      _n1 = n1;
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a doubt regarding POJO. Take below example public class User { String
I have a doubt regarding copy Overview: I have 2 classes namely Car and
I have a doubt regarding HttpServlet class is an abstract class even though there
i have a doubt regarding sizeof operator Code 1: int main() { int p[10];
I have one doubt regarding casting. public void Test(out T a, out T b)
I have a doubt regarding the backlog value in listen system call. From man
I have a very basic doubt regarding the method that gets executed when app
Possible Duplicate: What is the difference between const and readonly? i have doubt in
I have basic doubt regarding executable stored in ROM. As I know the executable
I have one doubt regarding audio processing for noise reduction. Is there any free

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.