Skip to content
Ameen Abozaid
BEG-025

Relationships

  • 5 Lessons
  • 49 min of reading
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Association
  • aggregation
  • composition
  • dependency

Learn first

49 min of reading · 2 h of practice (estimate)

Learning outcomes

  1. Distinguish association, aggregation, composition, and dependency, with Java examples.
  2. Draw a simple UML class diagram showing relationships and multiplicities.
  3. Implement one-to-many relationships in code.

Lessons

  1. 01How objects know each other10 min read
  2. 02Association, aggregation, composition9 min read
  3. 03Dependency10 min read
  4. 04UML class diagrams, the useful 20%10 min read
  5. 05Demo: drawing the UML, then coding Customer and Account10 min read

Lab and exercises

Lab

BEG-025-LAB1 · Bank Accounts v4

Outcomes: 1, 2, 3 · Time: ~1.5 h Draw the UML diagram first: Customer owns many Accounts; each Account has many Transactions (composition). Then implement it. Name each relationship type in a note. Acceptance: diagram matches code.

Exercises (assessment: diagram-from-description exercise)

IDOutcomeTask
BEG-025-EX011Classify: (a) Order has OrderLines, (b) Team has Players, (c) Printer used by a method parameter, (d) Student attends Course.
BEG-025-EX021Give Java code that shows aggregation and code that shows composition.
BEG-025-EX032Diagram from description: "A library has many books. A member can borrow many books; a book is borrowed by at most one member at a time. Each loan has a date."
BEG-025-EX043Implement Customer with a List<Account> and openAccount / accounts() that does not expose the list.
BEG-025-EX052, 3Spot the problem: Customer and Account each hold the other, but Account is never given its customer. Fix it.
Show solutions
  • EX01 (a) composition; (b) aggregation; (c) dependency; (d) association. Aggregation versus association can be debated; justification matters.
  • EX02 Aggregation: a Team receives existing Player objects in add(Player p). Composition: an Order creates its OrderLine objects internally and never shares the list.
  • EX03 Boxes: Library, Book, Member, Loan. Library 1 ◇— * Book; Member 1 — * Loan; Book 1 — 0..* Loan (only one open at a time); Loan holds the date. Judged on correct multiplicities.
  • EX04 public List<Account> accounts() { return List.copyOf(accounts); } and openAccount creates and adds.
  • EX05 Set both sides in one method (openAccount creates the account with this as owner), or remove the back reference if no one needs it.
  • Outcomes 2 and 3 are also covered by the lab.

Quiz

Outcomes: O1 relationship types · O2 UML · O3 one-to-many in code.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Parts that cannot exist without the whole show:O1
  2. 2A class that only uses another as a method parameter has a:O1
  3. 3A Bank holding Customers who exist independently is best called:O1
  4. 4An Account and its Transactions (deleted together) is:O1
  5. 5A filled diamond in UML means:O2
  6. 6A dashed arrow in UML usually means:O2
  7. 7Multiplicity * means:O2
  8. 8A one-to-many link is implemented with:O3
  9. 9A risk of bidirectional links is:O3
  10. 10A good UML class box shows:O2

Practical part

Diagram-from-description (BEG-025-EX03), the other exercises, and Bank Accounts v4 with a matching diagram. Pass: correct classes, relationship types, and multiplicities in the diagram, and code that matches it.

Common mistakes

Bidirectional links that are never kept in sync; UML with every field listed.

Assessment

Diagram-from-description exercise.

Career relevance

Entity relationships (JNR-030) and relational modeling (BEG-049).

Type to search levels, phases, modules and lessons.