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

Related Questions

I have a class ComponentItem which i simplified (pseudo code) below public class ComponentItem
i've created a socket extension class. the pseudo code below tries to make a
Please see my pseudo-code below. The code comments should explain my problem. I'm new
Hi I have below pseudo code with throws an exception like this throw new
I have Depth first searching algorithm whose pseudo code is given below: DFS(Vertex v)
Code below is working well as long as I have class ClassSameAssembly in same
The code below fails on the line: Class.forName(oracle.jdbc.driver.OracleDriver); with the error: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver The
Note: Sorry for the amount of pseudo code below, but I didn't know how
My question is in regard to the situation in the pseudo code below, for
I tried 2 things: (pseudo code below) int arr[10000]; for (int i = 0;

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.