Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 3

Composition: Build objects from other objects

  • Print
  • Feedback

Page 3 of 3

When should you use composition? As a rule of thumb, the presence of either "has" or "have" in specifications indicates the need for composition. For example, "a vehicle has an engine" or "timepieces have digital and analogue displays" indicate composition. By the way, when you browse through object-oriented-programming literature, you'll often come across the term has a relationship. That term originates from the phenomenon of an object having another object. In other words, an object composes another object. And, in a nutshell, that is all there is to composition!

Review

This article explored composition, an object-oriented design concept for building objects from other objects. You learned that composition relates objects to other objects and prevents disconnection between objects that should be intimately related.

Next month's article explores inheritance.

About the author

Jeff Friesen has been involved with computers for the past 20 years. He holds a degree in computer science and has worked with many computer languages. Jeff has also taught introductory Java programming at the college level. In addition to writing for JavaWorld, he wrote his own Java book for beginners -- Java 2 By Example (QUE, 2000) -- and helped write a second Java book, Special Edition Using Java 2 Platform (QUE, 2001). Jeff goes by the nickname Java Jeff (or JavaJeff). To see what he's working on, check out his Website at http://www.javajeff.com.
  • Print
  • Feedback

Resources