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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:43:34+00:00 2026-05-14T21:43:34+00:00

Code below is pseudo code. Imagine a class Fruit which has a factory method

  • 0

Code below is pseudo code.
Imagine a class “Fruit” which has a factory method to create a fruit.

interface Fruit
{
}
+(Fruit*) createFruit:
{
  return [[Fruit alloc] init autorelease];
}

Now I want to subclass the Fruit to get an Apple:

interface Apple : Fruit
{
int iSeeds;
}
+(Apple*) createAppleWithColor: (int) iSeeds
{
Apple* oApple = [Apple createFruit:];
oApple.iSeeds = iSeeds;
return oApple;
}

Questions:

  • How can I make “iSeeds” private so it cannot be changed from outside? If I add a “private” keyword it does not build anymore.
  • Still I want to set iSeeds from inside my Apple’s factory method.
  • I want users allow to READ the content of iSeeds. So I suppose I should have a getter but I can’t get it to work. I always get some error about “LValue assignment”.
  • The Fruit’s createFruit is making use of autorelease. Does the Apple have to reatin/release anything?

René

  • 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-14T21:43:35+00:00Added an answer on May 14, 2026 at 9:43 pm

    A couple things.

    1. Your createFruit method is wrong. It should be:

      + (Fruit *) createFruit {
        //autorelease, because the Create Rule only applies to CoreFoundation functions
        return [[[Fruit alloc] init] autorelease];
      }
    2. Instance variables are @protected by default. That means they can be directly accessed by the class and any subclasses. You can make it @protected, @private, @public, and @package. You do so like this:

      @interface Apple : Fruit {
        @private
        int iSeed
      }
      ...
      @end
    3. If you want an instance variable to be readonly, then don’t declare it as @public, but make a getter for it. For example:

      - (int) iSeeds {
        return iSeeds;
      }
    4. However, since the instance variable is readonly, you can’t set it externally. The way around this is to give the Apple a iSeed value during initialization:

      - (id) initWithSeeds:(int)aniSeedValue {
        if (self = [super init]) {
          iSeed = aniSeedValue;
        }
        return self;
      }

      Then make your createAppleWithColor:(int)iSeeds method like:

      + (Fruit *) createAppleWithColor:(int)iSeeds {
        return [[[Apple alloc] initWithSeeds:iSeeds] autorelease];
      }
    5. Finally, you should read the Naming Conventions guide and the Memory Management guide.

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

Sidebar

Ask A Question

Stats

  • Questions 527k
  • Answers 527k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I got it working now. I extracted all files to… May 16, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer If you want to use Parallel.For, you need to some… May 16, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer Use PropertyInfo.PropertyType to get the type of the property. public… May 16, 2026 at 10:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hi I have below pseudo code with throws an exception like this throw new
How would I achieve the pseudo-code below in JavaScript? I want to include the
I tried 2 things: (pseudo code below) int arr[10000]; for (int i = 0;
Note: Sorry for the amount of pseudo code below, but I didn't know how
the code below are all height=100%, it works fine in chrome but it displayes
I've got this code below, where I spawn several threads, normally about 7, and
I'm using the code below to to write to a file but at the
I expected the code below to print Hello, world! every 5 seconds, but what
Please see the code below: #include <windows.h> int main(int argc, char* argv[]) { HANDLE
The test code below leads to a null pointer deference bug on a String

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.