Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised January 9, 2026)

Previous: Coding Exercises: 2D Arrays

11. Composition

In this chapter we explore the composition of classes, where objects of one type contain references to objects of other types.

Whereas inheritance expresses the “is a” relationship between two classes, composition expresses the “has a” relationship.

In the first example, a PetOwner “has a” Pet object. In the second example, a CrazyPetOwner “has a” list of Pet objects.


Next: