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

Introduction to Class Inheritance (Delphi OOP Tutorial) Chapter 5
Help on the VCL Hierarchy; Comments on Inheritance; Homework Problems

By , About.com Guide

The online Help provides a lot of documentation on the VCL Components. To explore this, place a Timer on a form, select the Timer by clicking on it once with the mouse, and press <F1> for Help. A short description appears.

Near the top of the display is a list: Hierarchy, Properties, Methods, Events, Using TTimer.
If we select the Properties link, we see a display of the properties that TTimer itself declares and those it inherits from TComponent. We can similarly get a display of TTimer’s methods. This is a much longer list than the properties, and if we scroll down through the list we see methods from each of its ancestors.

Some Concluding Comments on Inheritance

In this chapter we have been looking at inheritance for subclassing. There is also another form of inheritance called subtyping. Subtyping is linked to the concepts of late (or dynamic) binding and polymorphism.

Polymorphism (and subtyping) are important and powerful aspects of object orientation that allow us, for example, to introduce additional subtypes into a system with minimal impact on the rest of the program, so simplifying future changes.

Multiple Inheritance
Delphi, like Java and unlike C++, does not implement multiple inheritance. To achieve an effect similar to multiple inheritance in Delphi or Java, we can use composition and/or interfaces.

Composition – an Alternative to Inheritance
While inheritance is very powerful and useful, it is not the only the way to re-use existing classes, and in some situations composition is preferable. Composition is a way to combine a group of objects to create a ‘super-object’. Delphi uses it extensively in generating user interface forms In example 1.2, for instance, the user interface (class TfrmStructureDemo) is derived from the TForm type and contains two Buttons and two methods (the event handlers). We can see this by looking at the type declaration in that program:


type
  TfrmStructureDemo = class(TForm)
   btnYellow: TButton;
   btnBlue: TButton;
   procedure btnYellowClick(Sender: TObject) ;
   procedure btnBlueClick(Sender: TObject) ;
end;
btnYellow and btnBlue are part of the TfrmStructureDemo class: TfrmStructureDemo HasA btnYellow and btnBlue.

In summary, building a class from one or more other classes is called composition. The aim behind composition is similar to inheritance – to be able to re-use existing objects and classes and so to write code only for additional characteristics. In OO, composition and inheritance are two major tools for creating new classes and for reuse.

Summary of Class Inheritance (Subclassing)

Subclassing facilitates reuse through programming by difference since the subclass IsA superclass. All the superclass’s data fields and methods are available to its subclasses (except for data and methods in the superclass that have private scope). Since the subclass inherits the superclass’s functionality it needs to implement (only) the additional data fields and methods and the override methods.

Uses of class inheritance

Extension: With extension, the subclass adds new functionality (MoreData, MoreMethods and ExtraData, ExtraMethods) but makes no changes to the superclass’s existing functionality.

Specialisation: With specialisation, the subclass is a special case of the superclass and so extends or overrides some of the superclass’s methods (OverrideMethods1 & OverrideMethods2). Excessive use of specialisation may indicate the need for a redefinition of the hierarchy, particularly where it completely replaces the superclass’s behaviour, since this invalidates the subtyping.

Generalisation: For generalisation, create a (possibly artificial) superclass. Remove the common data and functionality found in the subclasses and implement these in the superclass (generalise up). Do this repeatedly between each set of levels in the hierarchy. The consequence is that the common functionality, ie the generalisations common to the subclasses, is found in the superclasses. The specific functionality, ie the extension and/or specialisation, is found in individual subclasses.

Although we have treated these three different types of subclassing separately, they are often combined in practice. It’s quite possible and common for a single subclass both to extend a superclass and to specialise an aspect of the superclass, and for the superclasses to have been identified through a process of generalisation.

Problems

Problem 2.1 Using Help
Example 2.2 step 2 includes the code:


if Button = btNext then
  Form1.Top := Form1.Top - 20
else
  Form1.Top := Form1.Top + 20;
What is the significance of btNext? How can you find this out using Help? How would you recode this If statement so that the Then part instead adds 20 and the Else part subtracts 20 to give the same functionality as this?

That's it for the first 5 chapters!

Download Chapters 4-5 as a PDF document. Part 3 : Programmer Defined Classes and Objects

Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. 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) Chapter 5

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

All rights reserved.