Some final comments
The concept of substitution is that when we declare a variable as a particular type, that variable can refer to an object of the declared type or of any descendant types. Syntactically, this means that we can use an object of a subclass whenever an object of any of its ancestor classes is specified. (If we override our superclass methods indiscriminately in our subclasses, or if we dont maintain meaningful generalisation in our hierarchy, we can damage the semantics of substitution. But this is the subject of the next chapter, and only arises through poor programming practice.)There is a relationship between generalisation (mentioned in part 2) and substitution, and it is worth returning briefly to the concept of generalisation. Generalisation is the process of identifying the commonality that exists between a set of classes and then moving this commonality into a superclass. (We may create a superclass specially for the generalisation.) Since the common functionality from a group of subclasses has moved into a superclass, it makes sense that any of the subclasses can substitute for the superclass (though not the other way round). This possibility of substitution is an important reason for introducing generalisation into the applications class structure wherever possible.
In the VCL components, the Delphi developers have already designed the class hierarchy and produced several levels of generalisation. This is why we could use the substitution as we did in this chapter, with a SpeedButton, panel, label or form substituting for a TComponent. When we begin designing our own classes it will be well worth while to keep the concepts of generalisation and substitution in mind.
This is a brief chapter that simply illustrates the concept of substitution to prepare the way for polymorphism. In the next chapter we explore generalisation and substitution further, this time with programmer-written classes, and develop the concept to include polymorphism. Polymorphism is fundamental to several of the patterns well investigate a little later and is one of the main reasons for the growth and development of object-oriented programming.
Homework Problems and Soplutions; PDF and Code Download
This is the end of Part 5 of the Delphi OOP Course. Here's the full source code for the examples described in chapter 11.Forward to Part 6 / Chapter 12: Introducing type inheritance.

