Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   composition and inner classes (http://www.velocityreviews.com/forums/t389558-composition-and-inner-classes.html)

gaurav v bagga 12-20-2006 12:10 PM

composition and inner classes
 
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


Robert Klemme 12-20-2006 12:49 PM

Re: composition and inner classes
 
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

gaurav v bagga 12-20-2006 01:32 PM

Re: composition and inner classes
 
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


Thomas Hawtin 12-20-2006 01:58 PM

Re: composition and inner classes
 
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


All times are GMT. The time now is 06:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.