CHAPTER 35 — Objects that Contain Objects

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to home page   go to next page

creation: 4/18/99; revised: 10/01/99, 01/22/00, 07/16/02, 01/28/06

CHAPTER 35 — Objects that Contain Objects

One of the ideas of object oriented programming is to have software objects imitate "real world" objects. Most real world objects are made up of smaller objects. For instance, a bicycle is an object, and it is an assembly of several objects — frame, wheels, gears, handle bars, and other parts. Some of those parts are, in turn, made up of yet smaller parts. A wheel is made of a rim, a tire, an inner tube, a hub, and many spokes.

As with real world objects, software objects are often composed of smaller software objects. The syntax of Java makes this easy to do. This chapter shows how to do it.

Chapter Topics:

  • Defining a class that contains smaller classes
  • Example: Making a Fleet object out of Car objects
  • Example: Defining Fleet methods in terms of Car methods

QUESTION 1:

Could we continue to break down a bicycle into smaller and smaller objects?