Skip to content

Chapter 4 of 8

Object Relationships

Beyond the structure of individual classes, object-oriented systems are defined by how objects relate to one another. Association is the most general form of relationship, where one object uses or interacts with another without either owning the other. A Teacher and a Student, for example, are associated because they interact, yet neither contains the other. Associations can be unidirectional, where only one side knows about the relationship, or bidirectional, where both sides are aware of each other. Association is the weakest form of object relationship.

Aggregation is a specialized form of association that represents a whole-part relationship in which the part can exist independently of the whole. A Department containing Professor objects illustrates this idea: if the department were dissolved, the professors would still exist. In UML diagrams, aggregation is depicted with a hollow diamond on the whole side, signifying weak ownership. The relationship is therefore one of usage rather than true containment, representing a has-a relationship with weak ownership.

Composition represents the strongest form of whole-part relationship, where the part cannot exist without the whole. A House containing Room objects is a typical example, since rooms do not exist independently of their house. If the house is destroyed, so are its rooms. In UML, composition is shown with a filled diamond, and the parts share their lifecycle with the whole, representing strong ownership and a strict lifecycle dependency. The progression from association through aggregation to composition reflects an increasing strength of coupling, with association being the loosest and composition the tightest.

All chapters
  1. 1The Four Pillars of OOP
  2. 2The SOLID Design Principles
  3. 3Classes, Objects, and Language Mechanics
  4. 4Object Relationships
  5. 5Polymorphism and Method Dispatch
  6. 6Abstraction Through Interfaces and Abstract Classes
  7. 7Composition, Inheritance, and Code Reuse
  8. 8General Software Design Principles

Drill it

Reading is not remembering. These come from the Oop Principles deck:

Q

What is encapsulation in OOP?

Encapsulation is the bundling of data and methods that operate on that data into a single unit (class), while restricting direct access to some components.Key b...

Q

What is inheritance in OOP?

Inheritance allows a child class (subclass) to acquire the properties and behaviors of a parent class (superclass).Example in Java:class Dog extends Animal { }B...

Q

What is polymorphism in OOP?

Polymorphism means "many forms" — the ability of an object to take on different behaviors depending on its type.Two main types:Compile-time (static): method ove...

Q

What is abstraction in OOP?

Abstraction is the process of hiding complex implementation details and exposing only the essential features of an object.Achieved through:Abstract classes — pa...