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 7081071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:52:28+00:00 2026-05-28T06:52:28+00:00

I want to know if this is possible alloc child class from parent and

  • 0

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
  • 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-28T06:52:29+00:00Added an answer on May 28, 2026 at 6:52 am

    Not sure exactly what you’re asking, hopefully the answer is somewhere in what follows:

    Instance variables defined in an @interface have 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 @interface instance variables.

    Instance variables defined in an @implementation are 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.:

    @interface CHILDCLASS
    {
        PARENTCLASS *myCreator;
    }
    ...
    @end
    
    @implementation CHILDCLASS
    
    - (id) initWithCreator:(PARENTCLASS *)_myCreator
    {
       if((self = [super init]))
       {
          myCreator = _myCreator;
       }
       return self;
    }
    
    - (void) doSomething
    {
       NSLog("My parent's something: %@", something);
       NSLog("My creator's something: %@", myCreator->something);
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know what this looks like. I don't have any ideas about
I want to know this more in detail. Is it a realtime scheduler? An
I want to know if this web app are using long polling or anything
Ok, I know how to do this, but I want to know if this
I know this question isn't directly programming related, but since I want to be
I know this question is probably stoopid. But I just don't want to cause
I know there is this post , but I still want to know more
i want to import in my iphone application, but i know this framework is
The title is kind of obscure. What I want to know is if this
i just want to know a general information about this particular information. Any good

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.