In the previous chapter, you got hip to inheritance, a way to set up a relationship between two classes that removes the need for a lot of duplicated code. And we (briefly) mentioned that you can also set up relationships using composition, which is the subject of this chapter. You can use composition to combine objects so they can work together. In a typical program, you’ll use both inheritance and composition when creating your own classes, so it’s important to have a good handle on both concepts.
What Is Composition?
Composition in programming is like composition in music: you’re bringing individual components together and making them work to build something bigger. In music, you might bring together a bassoon part ...