File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Junior/Associate Certification (SCJA,OCAJ 5/6/8) and the fly likes Composition in Cam's Book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Junior/Associate Certification (SCJA,OCAJ 5/6/8)
Bookmark "Composition in Cam Watch "Composition in Cam New topic

Composition in Cam's Book

Brian Peasey

Joined: Oct 08, 2008
Posts: 1

On page 140 of Cam's study guide(2nd Ed.)there's an example UML diagram of composition and then a written explanation of how it's implemented in Java, but no actual example in java. Could someone post an code example based on this UML diagram? Thanks. (I understand composition in at the UML level but need to see this particular example in actual Java syntax)

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Well, UML is pretty near the implementation but here is my take on p140, in a very simple eample (the UML doesnt specify any access modifers or return types so I made all the methods default and the instance variable.

The main pont I think here is just to show the UML representation of Composition. The code doesnt really show more than the basic Association and Navigation (the getQuestion() Method). To represent composition in the code the constructor for the Question class would need a reference to an exam object or something similar to show that the lifecycle of the question(s) is dependent on an exam object.
[ October 26, 2008: Message edited by: Stephen Davies ]

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
I agree. Here's the link:
subject: Composition in Cam's Book
It's not a secret anymore!