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

Programmer Defined Classes and Objects - Delphi OOP Part 3 / Chapter 7
Subclassing TItem

By Zarko Gajic, About.com

Ex 3.3 step 1 Subclassing TItem

We’ll develop the code from example 3.2 to create the subclass of TItem. The first step is to define a subclass TItemBox. We can create this in either the existing ItemU unit or in a separate, third unit. For simplicity, we’ll extend the existing ItemU unit.
unit ItemU;

interface

type
4   TItem = class(TObject)
5   private
6    FCount: integer;
7   public
8    procedure AddItem;
9    function GetCount: integer;
10    procedure ZeroCount;
11   end;

  // Derived from TItem, not TObject
12   TItemBox = class(TItem)
13   public
14    procedure AddBox;
15   end;

16 implementation

17 { TItem }

18 procedure TItem.AddItem;
19 begin
20   Inc(FCount) ;
21 end;

22 function TItem.GetCount: integer;
23 begin
24   Result := FCount;
25 end;

26 procedure TItem.ZeroCount;
27 begin
28   FCount := 0;
29 end;

30 { TItemBox }

31 procedure TItemBox.AddBox;
32 const
33   NoInBox = 4;
34 begin
35   Inc (FCount, NoInBox) ;
36 end;

37 end.
Here, in addition to the TItem definition (lines 4–11) that we had previously, we define a new class called TItemBox (lines 12–15). While TItem is derived from TObject (line 4), TItemBox is derived from TItem (line 12). TItemBox is a subclass of TItem and so inherits all the data and methods that TItem has. This is a simple example of OO reuse – all the functionality of TItem is available in TItemBox without rewriting any of the code. TItemBox also has its own method, the procedure AddGoal declared in line 14.

In the implementation section (lines 16–36) TItem’s methods AddItems, GetCount and ZeroCount remain as before. Lines 31–36 define TItemBox’s method AddBox, which increments the value of FCount (defined in the superclass TItem).

Since TItemBox uses the data field FCount defined in TItem we don’t need to re-implement ZeroCount for TItemBox but simply rely on the inherited definition. Since inheritance works only in one direction TItemBox has TItem’s data and methods as part of its structure, but TItem does not have the method AddBox that is declared in TItemBox.

Although methods for both TItem and TItemBox appear in the implementation section of this unit Delphi distinguishes them by their headers since the class appears before the dot in their names. Notice that even although FCount is declared as private to TItems, TBoxItems can still access it. This is because the private, protected and public visibility specifiers in Delphi apply to the unit rather than to the class. From Delphi 2006, Delphi also has the strictly private and strictly protected that apply to the class whether or not it is packaged in the same unit as another class.

The class diagram now includes the new TItemBox subclass.

Ex 3.3 step 2 Using the new (sub)class

We add btnBox to the form. In unit OODriverU.pas we change the type of the object ItemCount to TItemBox (line 30 below), create ItemCount from TItemBox instead of TItem (line 52), and add an OnClick event handler for btnBox that calls the new AddBox method (lines 47–50).
26 implementation

27 uses ItemU;

29 var
30   // Change to TItemBox
30   ItemCount: TItemBox;

47 { Event handlers as before }
48 procedure TfrmCount.btnAddBoxClick(Sender: TObject) ;
49 begin
50   ItemCount.AddBox;
51 end;

initialization
  ItemCount := TItemBox.Create;

finalization
  ItemCount.Free;

end.
Save all, and then run and test this program.

Example 3.3 Summary:
In this example we derived TItemBox from TItem and added an AddBox method to TItemBox. Since TItemBox is a subclass of TItem, it inherits the definitions of TItem’s data fields and methods, and adds its own method definitions. (It could also have defined further data fields.)

By subclassing we do not interfere with the existing class TItem. Because this separate application class maintains the separation of concerns, the user interface does not need to know how many items there are in a box, but merely calls the AddBox method.

If, at some time in the future, the packaging changes and a box can hold six items, the user interface is unaffected since we instead change the unit defining TItemBox. Because of the inheritance, all of TItem’s functionality is available through TItemBox too.

Although we now declare ItemCount as a TItemBox and not a TItem, all the existing event handlers remain the same. We create a new event handler to use the added facility and this uses methods available only to a TItemBox and not to a TItem.

Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Programmer Defined Classes and Objects - Delphi OOP Part 3 / Chapter 7

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

All rights reserved.