1. Home
  2. Computing & Technology
  3. Delphi Programming

Introduction to Class Inheritance (Delphi OOP Tutorial)
Subclassing for Extension; Subclassing for Specialisation

By , About.com Guide

OOP Chapter 4 VFI Extended

In example 2.1 we didn’t 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 superclass’s 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 we’ll give each subclass its own separate method: they will each manipulate Form1’s 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) ;
begin
 inherited;

 if CheckBox1.Checked then Form1.Top := 10;
end;
There’s 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 – we’ll discuss the significance of ‘inherited’ in the next example.

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) ;
begin
 inherited;

 if Button = btNext then
   Form1.Top := Form1.Top - 20
 else
   Form1.Top := Form1.Top + 20;

 Form3.CheckBox1.Checked := False;
end;
Here again, Delphi automatically inserts the ‘inherited’ statement. We include a message from Form4 to Form3 to clear the CheckBox whenever Form4 repositions Form1.

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) superclass’s functionality.
Subclassing for extension is what people often have in mind when they extol the virtues of inheritance as an important approach to the problem of reuse. We also used this example as an opportunity to look briefly at bidirectional navigation between objects and at representing navigation on a class diagram.

Example 2.3 Subclassing for Specialisation

Another very important use of inheritance is subclassing for specialisation. Here, the subclass is in some way a special case of the superclass and either overrides or extends the superclass’s methods. We’ll give Form3 its own additional Button1Click event handler to extend the operation of the event handler it inherits.
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Introduction to Class Inheritance (Delphi OOP Tutorial)

©2009 About.com, a part of The New York Times Company.

All rights reserved.