Composite Objects
You’ve learned several ways to extend the definition of a class through techniques such as subclassing and using categories. Another technique involves defining a class that consists of one or more objects from other classes. An object from this new class is known as a composite object because it is composed of other objects.
For example, consider the Square class you defined in Chapter 8, “Inheritance.” You defined this as a subclass of a Rectangle because you recognized that a square was just a rectangle with equal sides. When you define a subclass, it inherits all the instance variables and methods of the parent class. In some cases, this is undesirable (for example, some of the methods defined in the parent class might ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access