Ex 3.3 step 1 Subclassing TItem
Well 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, well extend the existing ItemU unit.1 unit ItemU;Here, in addition to the TItem definition (lines 411) that we had previously, we define a new class called TItemBox (lines 1215). 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.
2 interface
3 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.
In the implementation section (lines 1636) TItems methods AddItems, GetCount and ZeroCount remain as before. Lines 3136 define TItemBoxs method AddBox, which increments the value of FCount (defined in the superclass TItem).
Since TItemBox uses the data field FCount defined in TItem we dont need to re-implement ZeroCount for TItemBox but simply rely on the inherited definition. Since inheritance works only in one direction TItemBox has TItems 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 4750).26 implementationSave all, and then run and test this program.
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.
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 TItems 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 TItems 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.

