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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:45:31+00:00 2026-06-18T00:45:31+00:00

I am trying to create an instance of a class, Dog , but the

  • 0

I am trying to create an instance of a class, Dog, but the compiler does not recognize the class and won’t instantiate it. I tried copying the initializers from Pet, the superclass, into Dog, but that didn’t make it work. Why can’t I instantiate the Dog object?

Pet.h

#import <Foundation/Foundation.h>

@interface Pet : NSObject
{
    // Some ivars
}

//Randomizer
+(id)randomPet;

//Initializer 
-(id)initName:(NSString *)name
initColor:(NSString *)color
initBreed:(NSString *)breed;

// Getters and setters...

@end

Pet.m

#import "Pet.h"

@implementation Pet

+(id)randomPet
{
   // Empty 
}

-(id)initName:(NSString *)name
    initColor:(NSString *)color
    initBreed:(NSString *)breed;
{
    self = [super init];
    if(self)
    {
        // Initialization...
    }
    return self;

}

@end

Dog.h

#import "Pet.h"

@interface Dog : Pet
-(id)initName:(NSString *)name initColor:(NSString *)color
    initBreed:(NSString *)breed;

// Getters and setters...
@end

Dog.m

#import "Dog.h"

@implementation Dog

+(id)RandomPet
{
    // Snip...
}

- (id)initName:(NSString *)name
        initColor:(NSString *)color
        initBreed:(NSString *)breed;
{
    self = [super init];
    if(self) {
        // Initialization...
    }
    return self;
}

main.m

#import <Foundation/Foundation.h>
#import "Pet.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        Dog *d = [[Dog alloc] init]];
    }
    return 0;
}
  • 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-18T00:45:33+00:00Added an answer on June 18, 2026 at 12:45 am

    You have following errors:

    1. Instead of #import Pet.h use #import Dog.h

    2. Dog *d = [[Dog alloc] init]]; should be Dog *d=[Dog alloc]init];

    3. No need to re-define same method in sub class as they are in super class.

    Apart from these answer, I would like to add few more things, these are not errors but it will add to your cocoa programming style and habbit :

    The method

    //Initializer 
    -(id)initName:(NSString *)name
    initColor:(NSString *)color
    initBreed:(NSString *)breed;
    

    does not go with standards, it should have been as :

    //Initializer 
    -(id)initName:(NSString *)name withColor:(NSString *)color withBreed:(NSString *)breed;
    

    In the following lines

    int petId;
    while(petId != 1 && petId!=2)
    

    What about initializing with petID=0;? If its garbage value become 1 then it will never enter the loop.


    You can use @property to create property.

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

Sidebar

Related Questions

I am trying to use reflection to create instance of a class. But it
I am trying to create a instance of class but I keep on getting
I'm trying to create an instance of a generic class using a Type object.
I am trying to create a generic class which new's up an instance of
I am trying to use the Class.forName('com.mypack.MyDomain').newInstance() to create an instance of the grails
I am trying to create instance of class by using reflection in ASP.net web
I am trying to create instance of class javax.servlet.ServletException with following code public class
I Trying to Create Instance by Using String name i.e a class name bt
I'm trying to create an instance of a class that I can add to
I am trying to create an instance of a singleton class inside of a

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.