composition and inner classes

G

gaurav v bagga

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
 
R

Robert Klemme

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
 
G

gaurav v bagga

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
 
T

Thomas Hawtin

gaurav said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top