Gagan_iOS
Gagan_iOS

Reputation: 4060

Modifying variable in Subclass

I have a query regarding subclass & modifying instance variable in Objective-C.

Suppose I have class say A with .h & .m

@interface A : NSObject
  @property(nonatomic, retain) NSString *myVariable;
@end

@implementation A
  -(void)viewDidLoad
    {
      [super viewDidLoad];
      self.myVariable=@"123";
    }
   @end

A has two subclasses Say B & C

 @interface B : A
  {
  }
 @end

@implementation B

  -(void)viewDidLoad
   {
     [super viewDidLoad];
     self.myVariable = @"Modified Here";
   }
@end

Now when I try to access 'myVariable' in subclass C

@interface C : A
 {
 }
    @end

@implementation C

 - (void)viewDidLoad
    {
      [super viewDidLoad];
       NSLog(@"Value is : %@",self.myVariable);
    }
@end

In log i found Value is : 123

Can anyone help me to understand why I am not getting updated value of var 'myVariable' which is being modified in class B.

Upvotes: 1

Views: 59

Answers (1)

Shefali Soni
Shefali Soni

Reputation: 1302

              A ("123")
              |
              |
        --------------
       |              |
       B              C
("Modified Here")  ("123") this is inherited from A

  So it prints "123"  

Upvotes: 1

Related Questions