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).