tags:

views:

1746

answers:

6

In Actionscipt, Parent means the object which contains the instance. So if a car has a wheel, wheel can tell the car to move forward with

parent.moveForward

However, in Obj-C, parent refers to the super class (not sure why they have super and parent mean the same thing).

I can't find any equivalent to the Action-script style parent, but then I don't really know what to even look for. Is there an easy way to do this in Obj-C, or do I have to manually pass a reference to the instance when I create it? I've tried this and It's throwing an error, but since I'm new to Obj-C, I just wanted to make sure I'm taking the right course of action.

+5  A: 

Interesting... I did not know that Actionscript used the word "parent" in that way.

There are two separate concepts at work here: inheritance and composition. The term parent is used in both cases in Objective-C and many other object-oriented languages.

In Objective-C, a class which inherits from another refers to that "other" class as its ancestor. While many people use the term superclass, in fact you could quite legitimately use the word parent. As the names imply, this is object "inheritance".

Object "composition", on the other hand, is what you've described with the example of the car and wheel. A car is composed of several other objects, one or more of which is the wheel. I must admit, I've not often heard the term parent used in this context to refer to the car. Usually, you'll probably find most people in the Objective-C world would recognize the car by the label "container". In the case of a tree-style data structure like an XML document, though, we often talk about parent nodes and child nodes. The parent contains the children, so using the word "parent" in a composition relationship is also apposite depending on the type of relationship. I don't think you'll find many Objective-C programmers using the "parent" word in composition scenarios outside of a tree structure, though.

The distinction is often likened to a "has a" versus "is a" relationship: identity versus possession. In inheritance, the child "is a" subtype of its parent. In composition, on the other hand, the car "has a" wheel, or a parent node in a tree "has a" collection of child nodes.

Edit: Kendall Helmstetter-Gelner below added some good comments on the part of your question related to instances and passing references. If you have a particular block of code and a specific error message, please feel free to update your question with them and we'll try to help.

Jarret Hardie
wow, great answer.
gargantaun
+2  A: 

To follow on to the composition answer - the usual pattern would be to have the parent either create or be given the objects it's holding onto, and then set itself as some kind of delegate for each one (if they need to reference the parent) - note that delegate references unlike most other things are almost never retained, usually assigned.

so in your example, the parent would tell each wheel:

wheel.carDelegate = self;

then the wheel could tell the parent:

[carDelegate moveForward];

Though frankly the metaphor in the example is losing me a bit there!

Another approach is to update variables in the contained obejcts that the parent would poll somehow (this approach for example can work well making views that get pushed using the navigation controller, and then return where the master view has to do something based on changes made in the sub views).

Kendall Helmstetter Gelner
Thanks for adding the bit on reference passing.
Jarret Hardie
+1  A: 

It isn't common to do this, at least not explicitly. In Cocoa, there are two facilities that I think take the place of the feature you're talking about:

  1. Delegation. You give the wheel a reference to another object that implements a certain set of methods. This could be the owner of the object, but it could be some other object that's slotted in place (great for testing). You can see this all through the Cocoa frameworks.

  2. Notifications. Nowadays this is most commonly done through Key-Value Observing. With this, Object A (the car in your example) registers to receive notifications when a certain property of Object B (the wheel) changes.

Chuck
+1  A: 

Research Key-Value Observation, or KVO. The Car can watch for changes in the Wheel and update itself.

Chris McCall
+2  A: 

Since you are talking about straight Objective-C and not a specific implementation of a framework (such as Cocoa), there are many ways to implement parent-child behaviour. The easiest of course is just to have your parent class object have multiple instance variables being the children. The children all accept methods like initWithParent: or initWithCar: in this particular case.

@interface Car : Object
{
    Wheel[4] wheels;
}

- (void) goFoward:(Wheel *) wheel;

@end

@interface Wheel : Object
{
    Car *parent;
    float tirePressure;
    // etc . . .
}

- initWithCar:(Car *) aCar;

@end

@implementation Car
- init
{
    self = [super init];
    if (!self) return nil;

    unsigned i;
    for (i = 0; i < 4; i++)
        wheels[i] = [[Wheel alloc] initWithCar:self];

    return self;
}

- (void) goForward:
@end

@implementation Wheel
- initWithCar: (Car *) aCar
{
    self = [super init];
    if (!self) return nil;

    parent = aCar;

    return self;
}
@end
dreamlax
+2  A: 

not sure why they have super and parent mean the same thing

Typical confusion :)

  • Super is the receiver, not the parent of its class. Just like self is not the receiver's class. The difference between self and super is where the method lookup starts from.

  • Parent is the pointer from a class to its superclass, which method lookup follows.

Damien Pollet