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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:40:48+00:00 2026-06-07T03:40:48+00:00

Just to give a real world example, say the base class is Vehicle and

  • 0

Just to give a real world example, say the base class is Vehicle and concrete classes are TwoWheeler and FourWheeler. Now the type of the vehicle – TwoWheeler or FourWheeler, is decided by the base class Vehicle. When I create an instance of TwoWheeler/FourWheeler using alloc-init method, it calls the super implementation like below to set the value of common properties defined in the Vehicle class and out of these properties one of them is type that actually decides if the type is TwoWheeler or FourWheeler.

   if (self = [super initWithDictionary:dict]){
     [self setOtherAttributes:dict]; 
      return self; 
    }

Now when I get a collection of vehicles some of them could be TwoWheeler and others will be FourWheeler. Hence I cannot directly create an instance of TwoWheeler or FourWheeler like this

Vehicle *v = [[TwoWheeler alloc] initWithDictionary:dict];

Is there any way I can create an instance of base class and once I know the type, create an instance of child class depending upon type and return it. With the current implementation, it would result in infinite loop because I call super implementation from concrete class.

What would be the perfect design to handle this scenario when I don’t know which concrete class should be instantiated beforehand?

  • 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-07T03:40:49+00:00Added an answer on June 7, 2026 at 3:40 am

    Generally this is done with a Factory.

    If you want the factory to be part of the base class, that’s fine but it may cause issues in the future. In Objective C, class methods make good factories.

    + (Vehicle *)vehicleWithDictionary:(NSDictionary *)dict
    {
        if ([[dict objectForKey:kVehicleType] isEqualToString:@"TwoWheeler"]) {
            return [[[TwoWheeler alloc] initWithDictionary:dict] autorelease];
        } else if ([[dict objectForKey:kVehicleType] isEqualToString @"FourWheeler"]) {
            return [[[FourWheeler alloc] initWithDictionary:dict] autorelease];
        } else {
            return [[[Vehicle alloc] initWithDictionary:dict] autorelease];
        }
    }
    

    The factory can be part of the Vehicle class and used as so.

    // Instead of [[Vehicle alloc] initWithDictionary:dict]
    Vehicle *vehicle = [Vehicle vehicleWithDictionary:dict];
    

    Update

    I came up with a way to do what was asked. Let it serve as a shining example of why it’s such a bad idea and why you should never do it.

    - (id)initWithDictionary:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            // If override is in the dictionary, then it mustn't try to call the subclass.
            if (![dict objectForKey:kOverride]) {
                NSMutableDictionary *overrideDict = [NSMutableDictionary dictionaryWithDictionary:dict];
                [overrideDict setObject:@"override" forKey:kOverride];
    
                if ([[dict objectForKey:kVehicleType] isEqualToString:@"TwoWheeler"]) {
                    [self release];
                    return [[[TwoWheeler alloc] initWithDictionary:overrideDict] autorelease];
                } else if ([[dict objectForKey:kVehicleType] isEqualToString @"FourWheeler"]) {
                    [self release];
                    return [[[FourWheeler alloc] initWithDictionary:overrideDict] autorelease];
                }
            }
    
            // init as normal
        }
    
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I were to say the heck with it!, I could just give my
Possible Duplicate: Real world use of custom .NET attributes Just like the title said
Can anyone please give me a real life, practical example of Polymorphism? My professor
I do not know how to ask this question. So I will just give
Just to give a little background: I'm using Dreamweaver CS5 for coding php, XAMPP
Just to give you a fair idea, I am new to the web development
My code isn't really that relevant but just to give you background, I have
I have written a Perl script, I just want to give it to every
I just had a test on java and we had to give the definition
I'm just having an issue I'm hoping someone will be able to give me

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.