Bifalgorithm post · Computer science one · Composition

How does composition model objects made up of other objects?

How does composition model objects made up of other objects? (Computer science one · Composition) — "Another important concept when designing classes is compos…

Ideas in this flowchart

  • Composition in class design
  • Composition makes an object out of other objects.
  • An object can own an instance of another object.
  • The Student class owns two instances of the String class.
  • The Student class can be extended with a date of birth.
  • A date of birth contains multiple pieces of data, such as a year, month, date, time, or locale.
  • It is generally best to use the language's existing date and time facilities instead of designing a custom class.
  • Java 8 introduced the java.time package with improved date and time classes.
  • The LocalDate class can model a date of birth.
  • Example declaration: private LocalDate dateOfBirth;
Feed/

How does composition model objects made up of other objects?

No cover

Channel

Computer science one

1 page

"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;"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.