I want to know if this is possible
alloc child class from parent and use parent class instance varriable
for example, I have a parent class called PARENTCLASS and a child class CHILDCLASS. If I do something like below it doesn’t work because the CHILD class is allocated and parent varrible are nil.
@interface PARENTCLASS : NSObject
{
NSString *something;
}
@implementation PARENTCLASS
@synthesize something
- (void) doSomething
{
CHILDCLASS *c = [[CHILDCLASS alloc]init];
[c doSomething];
}
@end
and then in child subclass PARENT
@implementation PARENTCLASS
@synthesize somthing
- (void) doSomething
{
NSLog(something);
}
@end
Not sure exactly what you’re asking, hopefully the answer is somewhere in what follows:
Instance variables defined in an
@interfacehave one of the accessibilities:@public– anybody can access them@private– nobody other than the owning class can access them@protected– the owning class and any derived (child) class can access them (said the other way around a child class can access the protected instance variables of its parent, its grandparent, etc.). This is the default accessibility for@interfaceinstance variables.Instance variables defined in an
@implementationare private to that implementation except that interface builder can access them.Now the phrase “owning class” above means “any instance of the owning class”. So for example if you have two different instances of the same class they can access each other’s private instance variables. Also one of the instances can access the protected variables of the others parent. Etc. In other words, protected and private apply to the “family” of all instances of the classes and not to individual object instances.
So a simple answer to your question is: yes, a child can access its parent’s public and protected instance variables.
However in your question you have a method in the parent creating a new child and then calling a method on that child and you ask can it access the parent? But which “parent” do you mean? After you have created the child there are two parent instances around, the original one on which the parent method was called, and the one which is part of the child object just created. Two parent objects means two sets of parent instance variables, which set are you trying to access?
The newly created child can access its parents instance variables using
self. It has the rights to access the instance variables of the parent object which created it as well, but to use those rights it needs a reference to that creating parent. In your code example no reference to the creating parent is passed to the child so the answer is: no, your child cannot access the instance variables of its creating parent until, and if, it is passed a reference to that creator.Comment Followup
You could pass the creating instance as part of initialisation. E.g.:
Is this “good practice”? There is nothing wrong per se with an instance having a reference to its creating instance, so its not “bad practice”. However I do wonder in this case whether there is some confusion over inheritance. It is not clear why are you wishing to access the instance variables of the parent instance that created the child and not the parent instance variables which are part of the child? There is nothing wrong in doing this, but with current details I can’t say what you’re doing is “good” either.