Objective-C basics: subclassing and member variable accessability
- by Krumelur
Hi,
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é