liveBook · Manning

Superclass

As explained in Object-Oriented Software Design in C++

A superclass is a class from which a subclass inherits state and behavior.

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

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains inheritance, access control, polymorphism, contracts, and template algorithms, but defers a complete treatment of implementation trade-offs.
Examples
Demonstrates Book, Attributes, Mammal, Human, Cat, Shipment, Expedited, and International class hierarchies.
What it assumes
Assumes basic classes and member functions while defining inheritance, subclassing, access levels, and polymorphism.
What you can do after
Build it from scratch: define shared members, extend subclasses, override behavior, and use superclass pointers polymorphically.
How it connects
Connects inheritance with subclasses, polymorphism, encapsulation, abstraction, Open-Closed Principle, Programming by Contract, and Template Method.

Also explained in

Compare how 10 books explain superclass →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.