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

  • Home
  • SEARCH
  • 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 8820111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:31:38+00:00 2026-06-14T05:31:38+00:00

I have a class Parent and a class Child. Child derives from Parent. I

  • 0

I have a class Parent and a class Child. Child derives from Parent. I only instantiate the Child class, the parent gets never initialized directly, only by the child.
I need at least Parent to be a singleton. If that does not work, Child being a singleton is okay too.
I tried the following but that didn’t change it to a singleton:

static MySingleton *sharedSingleton;

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized)
    {
        initialized = YES;
        sharedSingleton = [[MySingleton alloc] init];
    }
}

The Child class calls [super init] to instantiate the Parent. Can you help me here?

EDIT: I add some background information for my solution (architecture): I have a web service client which has a connection to a web service. I have a base class (Parent) which has information about the connection (authenticated, how to connect etc) and I have different child classes which derive from this base class. The need for different child classes is that one child is responsible for a set of web service logic and another child is responsible for another set of web service logic. The problem is, I want the connection information to be singleton (because they all use only ONE connection, not many).

  • 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-06-14T05:31:40+00:00Added an answer on June 14, 2026 at 5:31 am

    If I understand you correctly what you are asking doesn’t make sense, you seem to be confusing relationship with inheritance. If you have:

    @interface Parent : NSObject ...
    @interface Child : Parent
    

    Then every Child you create is a Parent as well, indivisibly part of itself, there is no way to split the Parent from the Child – you have a single object. Using inheritance does not give you multiple children with a single shared parent.

    If you want many children to share a single parent then you need your Child class to have a Parent. For example:

    @interface Parent : NSObject ...
    
    @interface Child : NSObject
    
    @property (readonly) Parent *myParent;
    
    - (id) initAsChildOf:(Parent *)parent;
    
    ...
    
    @end
    

    used as:

    Parent *theParent = [Parent new];
    Child *firstChild = [[Child alloc] initAsChildOf:theParent];
    Child *secondChild = [[Child alloc] initAsChildOf:theParent];
    

    If you wish you can make Parent a singleton class (only one instance possible), or a shared instance class (one shared instance, other instances possible).

    With inheritance, as opposed to a relationship, it is hard (but not impossible ;-)) to derive from a true singleton class and if you do so the derived class is itself a singleton.

    You can derive from a shared instance class; and you could write the class (hard again) so that you can have one and only one instance which is not indivisibly part of a instance of a derived class.

    Note: the other answers so far have all provided you with code to produce a shared instance class, a true singleton class is a little more involved (you can find the code in Apple’s documentation) and often a shared instance class is sufficient.

    HTH and I’ve haven’t misunderstood your question completely!

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

Sidebar

Related Questions

I have a parent class and child class (inherited from parent). In the child
I have a parent and child class that both need to implement IDisposable .
If I have child class,the child class inherits all methods from the parent,but how
I have a MustInherit Parent class with two Child classes which Inherit from the
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
I have a parent class Parent and a child class Child , defined thus:
I have a parent class which contains a child object. I am using set
I have two entities: public class Parent() { public ICollection<Child> Children { get; set;
I have the following two models: class Parent < ActiveRecord::Base has_one :child, dependent: :destroy
In Django, when you have a parent class and multiple child classes that inherit

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.