Class inheritance defines an object's implementation in terms of another object's implementation. Thus class inheritance (or subclassing) allows for the re-use of data fields and methods, and is a mechanism for code and representation sharing. It is therefore largely a consideration that comes up during implementation and is the form of inheritance we used in the first few chapters of these notes.
In contrast, interface inheritance (or subtyping) describes when an object can be used in place of another. Subtyping is concerned about being able to substitute a child for a parent, and consequently about polymorphism. It is largely a design consideration though its implementation needs dynamic binding.
Subtyping allows a particular type or interface to be defined in a parent class. The various child classes then implement this interface in such a way that any child may take on the role nominally assigned to the parent. Client programs declare variables of the type of the parent only so that they can remain unaware of the specific child class which is dynamically bound at any particular moment to the parent type,
An important distinction here is the difference between static and dynamic binding. With static binding, the message initiator determines how the message will be carried out through addressing a specific class (the class declared) irrespective of the actual class of the receiver. With dynamic binding, the message initiator addresses a specific type at run time and the receiver performs its (own) implementation of the message.
What makes the difference between subclassing and subtyping a little difficult to grasp at first is that both depend on inheritance. So its difficult to see from just a quick glance at a set of class definitions whether subclassing, subtyping or both are being used. A useful clue is given by the virtual, abstract and override keywords, which usually indicate that subtyping is being used in some way or other. As we work further through these notes, the distinction will become clearer.
Part 6 Homework Problems
There are a dozen of homework problems included in the Part's PDF. Make sure you solve all of them.This is the end of Part 6 of the Delphi OOP Course. Here's the full source code for the examples described in chapters 12, 13 and 14.
Stay tuned, Delphi OOP Course is not finished yet ... forward to Part 7 / Chapter 15.
