aspose file tools*
The moose likes Beginning Java and the fly likes Comprehending Composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comprehending Composition" Watch "Comprehending Composition" New topic
Author

Comprehending Composition

Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
I'm trying to nail down composition.
As I understand composition, it models the "has a" relationship.
To this end I've worked up this little relationship:
Dog->Collar->Buckle, which essentially says that every dog has a collar which has a buckle.

Can someone please tell me why, when instantiate a new Dog object ( DogDemo.java ), omitted here, but it's a simple driver program, I am unable to access any of the attributes & methods of the objects which compose the new Dog object??
The classes are:

[ September 28, 2002: Message edited by: Peter Simard ]
[ September 28, 2002: Message edited by: Peter Simard ]
[ September 28, 2002: Message edited by: Peter Simard ]

PAS<br />peter@panvox.net<br />2b || !2b
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I am unable to access any of the attributes & methods of the objects which compose the new Dog object??

1) Dog does not have any methods.
2) Dog's member variables are declared as private to the Dog class and are not
exposed to the classes outside Dog.
3) The constructor Dog(Collar) does not
properly initialize a Dog instance. The
c member variable is not initialized to
a non-null value.
[ September 28, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
darn, hit tab button too quick!!!
see next post...
[ September 28, 2002: Message edited by: Peter Simard ]
[ September 28, 2002: Message edited by: Peter Simard ]
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
OK...now I'm totally confused.
If I set the class members to public in Dog, doesn't that defeat the purpose of accessor methods( information hiding )? With this is mind I added accessor methods to dog:

Secondly, i get nullPointerExceptions when i run the driver program

Also,
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
I see you've made some progress on this. Here is a modified version showing accessors, some hiding of internal fields, and use of final to help make sure you don't forget to initialized the internal fields in each constructor, which tends to cause null pointer exceptions. I wasn't sure whether there was a need for both Dog and Collar to have a buckle, so I gave it to Collar.
Also, note the use of standard toString() methods to make it easy to display info for debugging purposes.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I think you have to meditate on your design a little.
Does a Dog have a Buckle?
The Collar has a Buckle for sure.
If you make a Dog() with no Collar then you have
to provide means of giving it one (void setCollar(Collar)). To find out about a Dog's Collar
you need Collar getCollar(void).
Don't be afraid to screw up the paper, dunk it in the bin, and start again. Did you do it on paper in the first place? I recommend it. Then translate it to code.
Cheers
-Barry
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There are many ways to skin a dog...
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
Thanks guys,
Actually I was thinking more about the status of the buckle than who/what owns the buckle...I see your point. [To that end I'd still like to make buckle an object in and of itself though]
It's more an excercise for me to understand how to effectively abstract and then build up objects to make one larger object.
Let me study your answers a bit.
Pete
[ September 28, 2002: Message edited by: Peter Simard ]
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
I'm curious about the accessor methods which return the object references to b & c
In Dog we define methods Buckle() and Collar() which return references to the instantiated objects (Collar and Buckle) which are instantiated with a new Dog object. This is different than what I'm used to doing/seeing for accessor methods, I assume this is to enable us to drill down to the accessor methods contained in those instantiated objects as defined in their respective classes?
[ September 28, 2002: Message edited by: Peter Simard ]
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
You would want to call the accessors getBuckle() or buckle(), following the convention that field and method names start with lower case, and class names (and hence constructors) start with upper case.
Yes, these are "drill-down" accessors. If you like, you could provide convenience methods on the containing class that access info in the contained object, just as you did with getCollarColor() for Dog, calling collar.getColor() in one of your earlier posts. That hides the existance of Collar from the user of Dog, and helps avoids long expressions in source code.
Each approach has its place.
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
OK guys, i think I've got a handle on it now.
My null pointer exception was due to my re-declaring an object ref. in the Dog constructor of the type Collar with the same identifier.
Many thanks for all you help!!!
Pete
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comprehending Composition