A superclass is a general class whose subclasses inherit state and behavior. Subclasses can add their own members or override inherited behavior. Polymorphism selects the implementation belonging to the subclass object when code uses a common superclass pointer. section 1.8
Mammal, Human, and Cat
Mammal stores shared weight and height, exposes accessors, and provides protected sleeping behavior. Human and Cat inherit those parts while defining their own eating and performing behavior; Human also adds needs_glasses and read_book(). A tester uses both concrete objects and displays different results. section 5.8
Use a superclass deliberately:
- Place genuinely common state and behavior in the superclass, then extend it with subclass-specific members. section 1.8
- Hide implementation with private members and expose protected members only when subclasses need them; protected members remain hidden from other code. section 5.8
- Keep the superclass stable when its common responsibilities are well understood, while allowing subclasses to extend it under the Open-Closed Principle. section 5.8
- When subclasses override contracted functions, preserve substitutability: accept at least the superclass's valid inputs and return results within its promised guarantees. section 7.5
Figure 4.11 Superclass Book has subclasses Fiction, Cookbook, and Howto. Also, superclass Attributes has subclasses FictionAttrs, CookbookAttrs, and HowtoAttrs. This figure shows two ways of drawing generalization diagrams.
A UML generalization diagram makes the direction of the subclass-to-superclass relationship visible through an open-triangle arrow. section 4.4.3









