Concept
How does composition model objects made up of other objects?
ComputerScienceOne / Composition
"Another important concept when designing classes is composition. Composition is a mechanism by which an object is made up of other objects. One object is said to “own” an instance of another object. We’ve already seen this with our Student example: the Student class owns two instances of the String class.\n\nTo illustrate the importance of composition, we could extend the design of our Student class to include a date of birth. However, a date of birth is also made up of multiple pieces of data (a year, a month, a date, and maybe even a time and/or locale). We could design our own date/time class to model this, but its generally best to use what the language already provides. Java 8 introduced the java.time package in which there are many updated and improved classes for dealing with dates and times. The class LocalDate, for example, could be used to model a date of birth:\n\n1 private LocalDate dateOfBirth;"
Related Ideas
- How does composition allow objects to own instances of other objects?ComputerScienceOne · Composition
- What is composition in object-oriented class design?ComputerScienceOne · Common Methods
- What does the completed PHP Student class look like?ComputerScienceOne · Example
- How can one structure be composed of another structure?ComputerScienceOne · Declaration & Initialization
- How can adding an addCourse method make a Student object more flexible?ComputerScienceOne · Example
- How can a Student object become more flexible?ComputerScienceOne · Example
- How can a date be modeled using a nested structure?ComputerScienceOne · Nested Structures
- How does encapsulation group and protect related data?ComputerScienceOne · Encapsulation & Objects