In example 2.1 we didnt add any code to units 3 or 4. Because of inheritance each of their Buttons uses the same event handler, the one inherited from the base form. However, a subclass can also extend the superclasss functionality. Subclassing for extension involves giving a subclass further features in addition to those it inherits.
Example 2.2 Subclassing for Extension
In this example well give each subclass its own separate method: they will each manipulate Form1s position.We reflect the bidirectional navigability on our UML class diagram through double openheaded arrows on the association links (figure 5). Here we show the relevant attributes and methods that extend the superclass. To present a slightly different style, the inheritance paths are shown as separate arrows, and not as combined tree graphs as in the previous class diagrams.
Ex 2.2 step 1 Extension in a Subclass
Start with the program of example 2.1. Add a TCheckBox to Form3 (figure 6), give it the Caption Top, and create an OnClick event handler by double-clicking on the CheckBox:procedure TForm3.CheckBox1Click(Sender: TObject) ;Theres a slight difference here from our previous event handlers. Since TForm3 is derived from TForm2 (and not TForm), Delphi automatically inserts the "inherited" statement as the first program line in this event handler. Leave it there for now even though it does not do anything here well discuss the significance of inherited in the next example.
begin
inherited;
if CheckBox1.Checked then Form1.Top := 10;
end;
TForm3 now inherits a button and event handler from TForm2 and adds a CheckBox and another event handler of its own.
Ex 2.2 step 2 A Different Extension in Another Subclass
An important aspect of subclassing for extension is that each subclass can extend the superclass differently. To see this, add an UpDown component (Win32 tab) to Form4, set its Wrap property to True, and create an OnClick event handler:procedure TForm4.UpDown1Click(Sender: TObject; Button: TUDBtnType) ;Here again, Delphi automatically inserts the inherited statement. We include a message from Form4 to Form3 to clear the CheckBox whenever Form4 repositions Form1.
begin
inherited;
if Button = btNext then
Form1.Top := Form1.Top - 20
else
Form1.Top := Form1.Top + 20;
Form3.CheckBox1.Checked := False;
end;
Ex 2.2 step 3 Bidirectional Communication
When using VFI we often send messages from one form to another, and in the steps above both Form3 and Form4 communicate with Form1, and Form4 communicates with Form3. We therefore need links between forms, and Delphi can often set these links up for us. To see this, run the program (press <F9>). Delphi issues a series of messages informing us that various form use other forms but do not include them in their respective uses lists. Click Yes each time for Delphi to insert the appropriate uses clauses. Notice that Delphi inserts local uses clauses into Unit3.pas and Unit4.pas (ie in the implementation section).Attempt again to run the program. This time, when you show Form3 and Form4 by clicking on Form1, each of these has the structure it derives from Form2 and also has its own, individual extension that we have just introduced.
Example 2.2 Summary:
This example introduces subclassing for extension. Using inheritance for extension has the characteristics that:
- The subclass inherits all the functionality of the superclass,
- The subclass adds new functionality,
- The subclass makes no change to the (inherited) superclasss functionality.


