1. Computing
Send to a Friend via Email

Introduction to Class Inheritance (Delphi OOP Tutorial)

Part 2 - Chapter 4


Visual Form Inheritance
Materials written by John Barrow. Modifications by Zarko Gajic

Back to Chapter 3

Introducing (in this Chapter):

  • Class inheritance.
  • Subclassing for reuse: extension, specialisation, generalisation.
  • Overriding inherited methods.
  • Encapsulation.
  • Visual Form Inheritance (VFI).


In part 1 (chapters 1,2,3) we talked about three perspectives on objects: objects as independent entities, objects as derived entities and objects as interacting entities. In this chapter we will take a first look at objects as derived entities by using one of Delphi’s RAD facilities called Visual Form Inheritance (VFI). This allows us to define a form and then use it as the basis for further forms through inheritance. We’ll use VFI to reuse an existing form through inheritance and then we’ll extend and specialise these derived forms through our own code. And because objects in isolation are not much use, we’ll look briefly at the interaction between these objects.

Example 2.1 Subclassing through Visual Form Inheritance (VFI)

In this example, we’ll use VFI to create the structure in figure 1. For now we show only the class names and suppress the attributes and the methods in the class diagrams. Later diagrams will show more detail.

Ex 2.1 step 1 The Master Form

Start a new application. We’ll write an event handler for this form (TForm1 in figure 1) to display other forms (TForm3 and TForm4) in a moment, but for now, just change the size of this TForm1 to about 300 x 150.

Note: These dimensions refer to a low resolution screen. With higher resolution screens 452 x 225 or 600 x 300 may be better. It just needs to be a convenient size: big enough to use easily and small enough not to obscure other forms.

Ex 2.1 step 2 The base class

VFI is the RAD process of creating a template form, the base class, and then inheriting (or subclassing) other forms from it. First we create the base class.

Using File | New | Form, add a second form to the project (TForm2 in figure 1) and place a button on it. This will form a base class from which we will inherit two subforms. Make the form a convenient size (about 200 x 100) and create an OnClick event handler for button1 in Unit2:

 13  implementation
 14  {$R *.dfm}
 15  procedure TForm2.Button1Click(Sender: TObject) ;
 17  begin
 17   ShowMessage ('Button ' + (Sender as TButton).Caption + ' clicked') ;
 18  end; // end procedure TForm2.Button1Click
 19end. // end Unit2 
The descendant forms (TForm3 and TForm4) will inherit this method, and we’ll use the Sender parameter to identify the Caption of the button that has been clicked.
Line 17 is the first appearance of the Sender parameter and we’ll see it several times again through the course of this module.

Ex 2.1 step 3 A Derived Form

TForm2, defined in step 2, is the base form from which we will derive further forms.

To create TForm3 in Delphi 4 to 7, select the menu sequence File | New | Other and then select the Project1 tab (or Project2, 3, .... depending on the name of your current project). To create TForm3 in Delphi 2006, select the menu sequence File | New | Other | Delphi Projects | Inheritable Items. Form1 and Form2 are listed. Select Form2 (this is the base form class from step 2), make sure that the Inherit RadioButton lower down is selected and click OK.

Up until now, all our forms have been derived from TForm. Because we are using TForm2 as a basis, Delphi derives our new form, TForm3 in Unit3, from TForm2 (line 7 below). Units are encapsulated from each other, and so for Unit3 to be able to refer to TForm2 in the class declaration (line 7), Delphi inserts Unit2, which defines TForm2, in the global uses clause (line 5) of Unit3.

unit Unit3;
 2 interfaceuses
 4   Windows, Messages, SysUtils, Variants, Classes,
 5   Graphics, Controls, Forms, Dialogs, Unit2;
 6 type
 7   TForm3 = class(TForm2) // inherited from TForm2, not TForm
 8   private
 9    { Private declarations }
 10   public
 11    { Public declarations }
 12   end;
 13   var
 14    Form3: TForm3;
 15   implementation
 16 {$R *.dfm}
 17 end. 
TForm3 inherits all the data fields (properties) and behaviour (methods and event handlers) from TForm2, and so a button, defined in TForm2, appears as part the screen display of object Form3 even though it is not listed in TForm3’s type definition (lines 7–12 above).

Using the Object Inspector, change the button’s Caption to btnForm3 and the form’s position (the Top and Left properties)so that it does not obscure either of the other forms. Don’t make any other changes to Form3 at this stage.

©2014 About.com. All rights reserved.