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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:26:54+00:00 2026-06-01T22:26:54+00:00

This may well be a stupid question, but here it goes anyway. In objective

  • 0

This may well be a stupid question, but here it goes anyway. In objective c, in an initiation method of a child class one often sets self=[super init]. Now, self is of the child class, and no conversion is required, which means (or not..) that the init method of the parent class somehow returns an object which actually belongs to the child class.

So my question is: can I define other methods or properties in a parent class, which when called from a child class return an object from the child class? There is something similar in Java called factory methods I think, so perhaps this question is not too outrageous..

Edit 1. Let me better explain what I mean: I know that I can find out in a ParentClass method, that I’m actually operating on an object from class ChildClass. This is by looking at [self class]. However, I would like to declare a method in ParentClass that returns an object from the child class, without knowing which class the child will be in advance. Better yet, I want the returned object to not even need formal conversion.

For example, I want a method in the parent class that does something like the following:

-(ChildClass *) partialCopyOfSelf {
//returns an object which is a partial copy of self (only copying properties that also
// exist in parent class, but such that applying [result class] will yield the child class
// and will require no further conversion

(ChildClass *) copy=[[ChildClass alloc] init];
//now copy stuff
return copy;
} 

Now in the child class, I want to be able to implement something of the following sort:

-(MyClass *) copyOfSelf {
MyClass *copy=[super partialCopyOfSelf];
// now copy properties that only exist in my child class
return copy;
}

Edit 2 (from a comment I wrote to @wbyoung). The reason I want this is that at some point i may want to change the parent class’ properties from ivars to perhaps just computing some values for example, so a child would not know how to copy them correctly. I am writing this since someone may have a better idea of how to accomplish the same task.

  • 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-01T22:26:56+00:00Added an answer on June 1, 2026 at 10:26 pm

    The init method is actually just working with the object that’s already been created of the child class. It’s the call to alloc that does this. That’s using the class to which you sent the message to figure out how to allocate the object.

    That being said, you can override methods in your child class. Messages are always sent to the object & method lookup starts at the top most class.

    You could also do something like [self class] to figure out your type in the parent class. That will only get you so far, though. [[self class] displayColor] would allow you to do something like implement a method & default class method implementation in the base class, but override a class method in subclasses as needed.

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

Sidebar

Related Questions

I know this question may well be the silliest question you've heard today, but
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
This may seem like a stupid question, but I've been looking everywhere to fix
Well, I'm working on a small project, and this may sounds stupid, but I
This may be a stupid question but ... If I use JQuery .load(); etc
This may be an obvious question, but I think there may well be multiple
This question may seem stupid, but, so be it! I have a stupid problem
this may well be a very basic question, but I haven't been able to
This question may well have been asked before but I didn't find anything whilst
This may well be simple, but I'm new to Drupal. The organization I work

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.