Objects in objects

Hi,

I have a mostly theoretical question about objects that have objects (or contain an ArrayList of objects). Inside the nested objects, there’s no ‘built in’ way of getting to the original object, right? I can solve this by sending IDs or so, but I don’t quite get why there’s no cleaner way to do it. Example:

class Apple{
  String description="apple";
  Apple(){
  }
  void saySomething(){
    println("I'm an", description, "and I live in a", [some sort of reference to the thing that 'own' me].description );
  }
}

class Tree{
  Apple a;
  String description="tree";
  Tree(){
    this.a = new Apple();
  }
}

This code won’t run, it’s just to illustrate the feature I keep feeling is missing. What am I missing? Shouldn’t this sort of referencing be possible? Why can I say “The apple that is in this tree”, but not “The tree this apple is in”?

/Henrik

1 Like

In general an object shouldn’t need to be aware of any other object which “owns” it.

But if you feel you need such feature you can create an “owner” field for it:

/**
 * AppleTree (v1.0.0)
 * GoToLoop (2020/Jul/08)
 * https://Discourse.Processing.org/t/objects-in-objects/22486/2
 */

import java.util.List;

Tree tree;

void setup() {
  tree = new Tree().bearFruit().bearFruit();
  println(tree);
  println(tree.apples);
  exit();
}

class Apple {
  Tree owner;

  Apple() {
  }

  Apple(final Tree tree) {
    owner = tree;
  }

  String toString() {
    return "I'm an " + getClass().getSimpleName() + " from " + owner;
  }
}

class Tree {
  final List<Apple> apples = new ArrayList<Apple>();

  Tree bearFruit() {
    apples.add(new Apple(this));
    return this;
  }

  String toString() {
    return "I'm a " + getClass().getSimpleName();
  }
}
3 Likes

A bunch of neat stuff here that’s new to me, thanks! It’s prettier than how I knew how to do similar, for sure. But it is still you telling the apple what it belongs to, with several lines and declarations, where I would assume that the info being sent to the apple, should be there under the hood somehow. I was curious if there was a hard reason for having to do it ‘manually’. Or is it just convention, or that no one thought it was worth the effort?

Java objects are only aware of the class which instantiates them, not much else. :coffee:

1 Like

But in theory, even if the object aren’t aware, processing or java _could_surely have a findMeTheParentOfThisObject method, right?

  • A “parent” of any object is the class which instantiates it.
  • An object is by itself a reference to the 1st memory address of its memory block.
  • When we assign an object to a variable (be it a field, parameter or an array’s index slot) we’re actually assigning the object’s memory address value to it.
  • An object’s reference isn’t self-aware whether its memory address value is being stored in 1 or more variables.
  • The JVM’s garbage collector (GC) would be the only entity knowing such info.
4 Likes

I see! Wonderful answer, thanks a lot! Without knowing this, it’s easy as a noob to feel like ‘well, this object knows, so the info must be there somewhere.’, when you don’t know the under the hood stuff. :slight_smile:

Hi Carnalizer,

you can give Apple a Constructor that enforces to put in a pointer to the tree, who created the apple. Then you can ask any Apple anytime about who is his tree. That is should be done quickly and easy.

Apple(Tree parent)
{
  myParent=parent;
}
2 Likes

Or a list, for that matter. Keep in mind that you aren’t limited to containment – you can have one-one, one-many, many-one, or many-many relationships. Also, the method for registering that relationship can be through the constructor or through a method on either object if it is passed the other object as an argument.

class Event {
  ArrayList<Person> attendees;'
  attend(Person p) {
    attendees.add(p);
  }
}

class Person {
  attend(Event i) {
    i.attendees.add(this);
  }
}
2 Likes

Cheers Quertz! I do believe I’ll start doing it that way. :slight_smile:

Thanks Jeremy! Always been a bit unsure how to use ‘this’. Great example!