What makes this example different from the previous example is the presence of containment and inheritance. The CQueryHandler class can offer common services for both data persistence models, making substitution easy. Typically, CQueryHandler will be an abstract class. It will establish a useful set of public method signatures. Figure 7 would then incorporate inheritance, containment, and polymorphism.
Inheritance works well when one class is a specialized variant of another class.
CQueryHandler captures the commonality for handling queries. It accepts not only the method signatures, but also the method implementations. The Risks of Inheritance Inheritance clearly has important uses, but its important risks aren't so obvious. Inheritance provides strong encapsulation to other classes, as long as those classes are outside its own class hierarchy. Within a class hierarchy, encapsulation is weak.