1. Computing

Introduction to Class Inheritance (Delphi OOP Tutorial)

Visual Form Inheritance in Delphi

From , former About.com Guide

Introduction to Class Inheritance (Delphi OOP Tutorial)

Ex 2.1 step 4 Another Derived Form

We can inherit any number of classes from an existing class, so inherit another new form and unit (TForm4 in Unit4) from Form2 following the process outlines in step 3.

Change the Caption of the button on TForm4 to btnForm4. Position the four forms so that they do not overlap, but don’t change any other properties.

Inheritance means that the subclass inherits all the data fields and methods from the superclass. Because this is a RAD application, the subclasses also have as default the initial data values of the superclass. To see this, change, say, the width of Form2. The widths of Form3 & Form4 change as well. If you’re in any doubt about what we have done so far, study the code for each of these units.

Ex 2.1 step 5 Linking the forms

We now have an inheritance structure with a number of different classes. However, a class structure on its own does not do much – we need to introduce some interaction paths between the classes. We want to be able to display Form3 and Form4 from Form1. We can extend our class diagram to show these paths.

The UML convention uses closed, unfilled arrowheads for inheritance and open arrowheads for navigation links (also called association links). Figure 3 shows open arrowheads on the links between TForm1 and TForm3 & TForm4, pointing from TForm1 to the other two forms. These reflect that Form1 can access Form3 and Form4, but that no access is available in the reverse direction.

To implement these links, return to Unit1. Using the Component / Tool palette, place a button on TForm1. Set its Name property to btnShowForms and its Caption to Show Forms. Create an OnClick event handler for it that sends messages to Form3 and Form4, asking them to Show themselves (lines 20–21 below).

Press <F9>, ostensibly to run the program. Delphi will show an Information Box saying: “Form ‘Form1’ references ‘Form3’ declared in ‘Unit3’ which is not in your USES list. Do you wish to add it?” Click the Yes button for this question. Run it again. It repeats the question, but this time for Unit4. Click Yes to add Unit4 to the uses clause as well. Delphi adds a local clause ‘uses Unit3, Unit4;’ to Unit1 (ie in its private, implementation section and not in the public, interface section) (line 16 below).

 15 implementation
 
 16 uses Unit3, Unit4;
 
 17 {$R *.dfm}
 
 18 procedure TForm1.btnShowFormsClick(Sender: TObject) ;
 19 begin
 20   Form3.Show;
 21   Form4.Show;
 22 end;
 
Run the program again. Form1 (only) appears. Click on it to invoke its OnClick event handler and Form3 and Form4 appear. Form2 does not appear because it has not been instructed to Show itself. A Click on Form1 can send a Show message to Form3 and to Form4 because there is an association between TForm1 and TForm3 and between TForm1 and TForm4 (figure 3). This linkage is possible because of the uses clause in Unit1, which lists Unit3 and Unit4 (line 16 above).

Click the button on Form3 or Form4, and a message identifying the button by its Caption appears.

Neither TForm3 nor TForm4 have defined any data fields or operations. However they are both derived from TForm2, which defines a TButton data field and its OnClick event handler, and so both TForm3 and TForm4 inherit the button and its event handler from TForm2. Thus, through inheritance, TForm3 and TForm4 reuse TForm2 and in this case do not need any code of their own.

We set different Captions for the Buttons on Form3 and Form4 to show that we are working with different objects. To identify these separate objects, we use the Sender parameter in line 17 of Form2’s OnClick event handler (example 2.1, step 2). Since every instance has its own data, Form3 and Form4 also have their own values for Top, Left, and so on, which is why they can take up different positions on the screen.

Save this project as we’ll use it as a starting point for examples 2.2 and 2.3.

Example 2.1 Summary:
The main OO principles in this example are inheritance and communication. TForm2 inherits the knowledge of ‘how to be a form’ from TForm. It adds its own data field (a button) and a method (the button’s OnClick event handler). TForm3 and TForm4 are both derived from TForm2 and neither adds anything of its own. Yet, because of inheritance, each one has the knowledge of how to be a form (from the VCL class TForm), and a button and its event handler (from TForm2). We also created navigation links (simple association) between one form and another.

From a Delphi perspective, we have seen how with VFI we can define a base form (Form2 in this case) and then derive further forms from it. We gained all the functionality available in the base form without having to redefine forms separately as in chapter 1. The buttons on the derived forms even all invoke the base form’s event handler. So VFI is a useful illustration of OO and inheritance and the way subclassing can promote reuse.

©2013 About.com. All rights reserved.