Skip to content

Object Relationships

As mentioned previously, classes interact with one another to function in an objectoriented program.

There are four primary relationships between classes:

  • Association (“uses-a”)
  • Aggregation (“has-a”)
  • Composition (“owns-a”)
  • Inheritance (“is-a”)

Credits: the examples here come from a PTS reviewer made by Cai Benjamin Reyes and Diane Panganiban!

For practice, why not try coding the diagrams here into java code?

Association describes a relationship where one class uses another class, but both can exist independently.

Neither object owns the other as they simply work together.

A bidirectional association means both classes know about each other.

In the example below, the Plane knows which Airport it is currently in, while the Airport keeps track of the planes currently at the airport.

UML Example 5

Note: The airport is not responsible for creating or owning the planes. It simply keeps a record of which planes are currently there.

Since both classes are aware of each other, the relationship is bidirectional.

Sometimes only one class needs to know about the other.

In this example, the Plane knows about the Airport, but the Airport has no reference to the Plane.

UML Example 6

This is called a unidirectional association.

Key Idea: Association simply means one class uses another class. Neither class owns the other, and both can exist independently.

Aggregation describes a has-a relationship.

The parent class contains one or more child objects, but the child objects can still exist without the parent.

Think of it as a weak ownership relationship.

Example: A playlist has a list of songs.

UML Example 7

Note that deleting the playlist does not delete the songs, they can still exist in another playlist.

Key Idea: The child object has its own independent lifecycle.

If the parent disappears, the child can continue to exist.

Composition is a stronger version of aggregation.

It also represents a has-a relationship, but this time the child object cannot exist without the parent.

Example: A company owns teams

UML Example 8

Notice that the Company creates and manages its Team objects.

If the company is deleted, its teams disappear as well! (ded)

Key Idea: The parent completely owns the child. Destroying the parent also destroys the child.

The sample below shows a combination of aggregation and composition.

UML Example 9

A team is a part-of a company, and a team has-an employee.

Lastly, Inheritance describes an is-a relationship.

A child class inherits the attributes and methods of its parent class (super class).

UML Example 10

A Student is a Person.

A Professor is also a Person.

Both Student and Professor inherit the common characteristics of Person.

In Java, inheritance is written using the extends keyword.

class Person {
}
class Student extends Person {
// You can apply polymorphism here!
}
class Professor extends Person {
// You can apply polymorphism here!
}
// Wow advance study?!

Key Idea: Inheritance allows multiple classes to share common attributes and methods without rewriting code.

Each relationship has its own UML notation.

Relationship UML Symbol Meaning
Association Line (with or without arrowheads) Uses-a
Aggregation Hollow diamond ◇ Has-a (weak ownership)
Composition Filled diamond ◆ Has-a (strong ownership)
Inheritance Hollow triangle △ Is-a

Cheatsheet Cheatsheet by Khalil Stemmler

Some UML diagrams include numbers beside the relationship lines.

For example:

  • 1 -> Exactly one object
  • 0..1 -> Zero or one object
  • 0..* -> Zero or more objects
  • 1..* -> One or more objects

In the Playlist example, 1..* indicates that a playlist contains one or more songs.

These numbers describe how many objects participate in the relationship.

Tech tip: A quick way to remember them is:

  • Association: “I use it.”
  • Aggregation: “I have it.”
  • Composition: “I own it.”
  • Inheritance: “I am it.”

YOU’RE IT!!! NOW GO ACE THAT EXAM