composition and inner classes

Discussion in 'Java' started by gaurav v bagga, Dec 20, 2006.

  1. Hi all,

    As per the definition i found,

    With composition, the part object may belong to only one whole;
    further, the parts are usually expected to live and die with the whole.

    How to handle this in java, I saw few C++ codes where they handle
    deletes of child in destructor, in java should I implement an Inner
    Class ???

    I'll appreciate if any one can guide me.

    regards
    gaurav v bagga
     
    gaurav v bagga, Dec 20, 2006
    #1
    1. Advertising

  2. On 20.12.2006 13:10, gaurav v bagga wrote:
    > With composition, the part object may belong to only one whole;
    > further, the parts are usually expected to live and die with the whole.
    >
    > How to handle this in java, I saw few C++ codes where they handle
    > deletes of child in destructor, in java should I implement an Inner
    > Class ???


    Nesting of classes and nesting of instances are two completely
    orthogonal concepts. You do not need to have a nested class to nest
    part instances. Whether or not you create a nested class should be
    solely determined by the usage of that class, typically you do this only
    for classes that are only used and can only be used inside the
    surrounding class.

    Basically, if you want to ensure that an instance is to be discarded
    with the surrounding instance, you must not leak the object reference of
    the part to any client code. However, the question is whether it's
    necessary.

    Since Java has a completely different object model and memory model than
    C++ you cannot easily translate concepts. If you present more details
    about the problem you are trying to solve we can come up with
    suggestions how to tackle that in Java.

    If this is just a theoretical discussion of general concepts then I'd
    say formally speaking there is no way to ensure that a part lives no
    longer than the whole because of Java's garbage collection; even if you
    do not leak the reference of the part to the outside the GC is still
    free to collect the whole but leave the part alone. I'd also say that
    to me this seems far less of a problem in Java than it might be in C++
    exactly because of GC; so you just might not worry too much. :)

    Regards

    robert
     
    Robert Klemme, Dec 20, 2006
    #2
    1. Advertising

  3. hi,

    Thanks for reply.

    I dont have any specific requirement right now was just gowing through
    UML(assignment given to me),so thought came in my mind.

    I'll be glad if anyone can give me reference of some example code in
    java related to association,aggregation and composition, clearing above
    point or just explaining the concept of AAC.

    Meantime if I find some, i'll post them to clearify the doubts :).

    regards
    gaurav
     
    gaurav v bagga, Dec 20, 2006
    #3
  4. gaurav v bagga wrote:
    >
    > With composition, the part object may belong to only one whole;
    > further, the parts are usually expected to live and die with the whole.
    >
    > How to handle this in java, I saw few C++ codes where they handle
    > deletes of child in destructor, in java should I implement an Inner
    > Class ???


    In Java objects never really die. The memory used by objects gets
    reclaimed when the JVM can be sure that there is no way that object can
    ever be used again.

    Without the worry of destructors, there is little significance to
    'ownership'. And so things become very much easier.

    Tom Hawtin
     
    Thomas Hawtin, Dec 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999
    Replies:
    1
    Views:
    2,244
    VisionSet
    Nov 13, 2003
  2. Razvan
    Replies:
    5
    Views:
    11,328
    Dale King
    Jul 27, 2004
  3. Christian Bongiorno
    Replies:
    5
    Views:
    553
    Chris Uppal
    Aug 30, 2004
  4. Carlo v. Dango
    Replies:
    14
    Views:
    1,065
    Alex Martelli
    Oct 19, 2003
  5. Pyenos
    Replies:
    2
    Views:
    399
    Pyenos
    Dec 27, 2006
Loading...

Share This Page