Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am programming in Objective C, iOS. I create a Class A which is a parent class, and Class B inherits to Class A.

Now I have Class B variables which I also need to use in Class A. And also one another Class C, Class A add in Class C (not inherits), and I want to use Class B variables in Class C.

How can I access? I have created object also but not working, is that possible to access?

share|improve this question
Why can't you move ClassB's variables to ClassA? – Ozair Kafray Jun 7 '12 at 12:28
What are you trying to model? What you're trying to do seems to go against OO. Almost surely there is a better way to model your problem, which doesn't involve ClassA knowing anything about ClassB. – Andres F. Jun 7 '12 at 12:53
I m not sure that its possible or not, So that's why i am asking here, can i access variable of class B in class A ? – Jagdish Jun 7 '12 at 13:03
@Jagdish A piece of general asking advice: provide a lot of context. Enough people, reading this Q&A, can detect a rotten architecture from a mile and providing more context will help them guide you in the right direction on the meta-problem that made you post here in the first place :) – K.Steff Jun 7 '12 at 16:43
@K.Steff thnaks for giving me advice in right direction.. Next time i will keep in mind this point. thanks. :) – Jagdish Jun 8 '12 at 7:36
up vote 6 down vote accepted

Now I have Class B variables which I also need to use in Class A. And also one another Class C, Class A add in Class C (not inherits), and I want to use Class B variables in Class C.

How can I access?

Short answer: No, you can't do what you propose.

Longer, more detailed explanation: Classes don't have variables -- objects do. They're called instance variables (or ivars for short) because they're variables that are associated with an instance of a class, which is to say an object. You declare the variables in the class declaration, of course, but they aren't actually created until the class is instantiated, that is, until an instance (an object) of the class is created.

So, if you have this:

ClassA *objectA = [[ClassA alloc] init];
ClassB *objectB = [[ClassB alloc] init];

then objectA is an instance of ClassA, and objectB is an instance of ClassB. Let's also stipulate (as you have in your question) that ClassB is a subclass of ClassA. That is, it's declared like:

@interface ClassA : NSObject
    int foo;

@interface ClassB : ClassA
    int bar;

So to get to your question... objectA has an instance variable called foo, plus any that it inherits from NSObject. objectB has an instance variable called bar, and it also inherits foo from ClassA and whatever ClassA gets from NSObject. objectA does not have an ivar called bar, and trying to access it would cause an error:

objectA->foo = 1;    // OK
objectB->foo = 2;    // OK
objectA->bar = 3;    // Error!
objectB->bar = 4;    // OK

This is because any instance of ClassB is also an instance of ClassA (that's the effect of inheritance) but an instance of ClassA is not necessarily an instance of ClassB. It's okay for code in ClassA to refer to variables or properties in ClassB, but only if it knows that it's dealing with an instance of ClassB. So you could have a method like:

- (void)addBar:(ClassB*)someB
    foo += someB->bar;

That's okay because you know that someB is supposed to be an instance of ClassB. What you can't do in ClassA is:

- (void)addBar:(ClassB*)someB
    bar += someB->bar;    // Error! ClassA doesn't have a 'bar' ivar!

because, again, ClassA doesn't have an ivar named bar.

By the way, even the first example isn't great form -- unless there's a good reason, it's better to avoid the situation where a class (like ClassA) is dependent on its own subclass (like ClassB).

share|improve this answer

Is it possible to move ClassB's variables to ClassA?

If not then maybe you could add whatever the Obj-C equivalent of pure virtual functions/interface is to ClassA, that ClassB must implement, so that ClassA can use the variables. Then ClassC could also access them through this interface.

share|improve this answer
No not possible to move ClassB's variable to ClassA.. – Jagdish Jun 7 '12 at 12:05

Short answer:

That can't be done because parent classes are totally oblivious of it's children.

share|improve this answer
Thanks a lot. One Question for you, Class A : Class W Class B : Class W Now can pass method of Class B to Class A ? – Jagdish Oct 3 '12 at 14:55
That allows for a program that uses objects of type W, to call all methods defined in W on them, regardless of they beeing B or A under the hood. One problem here is that in your question you say "Class B inherits to Class A". You cannot inherit to, you only can inherit from. So the question is a little bit ambiguos since one cannot tell if you meant A is the superclass or the other way around. – Tulains Córdova Oct 3 '12 at 17:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.