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 model objects made up of other objects? | ComputerScienceOne | Bifalgorithm | Bifalgorithm