• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comprehending Composition

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many ways to skin a dog...
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic