Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > composition and inner classes

Reply
Thread Tools

composition and inner classes

 
 
gaurav v bagga
Guest
Posts: n/a
 
      12-20-2006
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-20-2006
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
 
Reply With Quote
 
 
 
 
gaurav v bagga
Guest
Posts: n/a
 
      12-20-2006
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

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      12-20-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Debate: Inner classes or public classes with package access? Christian Bongiorno Java 5 08-30-2004 08:14 AM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57