Delphi Programming

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

Using and Abusing Inheritance - Delphi OOP Part 7 - Chapter 16

By Zarko Gajic, About.com

Underuse of interface inheritance: Inheritance considered beneficial

One of the first concepts one learns about programming is conditional execution, at first through the If construct and then the Case construct, and so it is natural to use these constructs whenever conditional execution is needed. But Ifs and Cases are extremely rigid and explicit.

Undoubtedly If and Case are very important constructs, but with OO there are times when one can transfer the responsibility for acting appropriately to the objects. So the problem changes from needing to specify particular behaviour within an If or Case structure to taking advantage of polymorphism by assigning the responsibility to an appropriate object.

Anti-Pattern 7.5 Conditionals suspect

In situations where classes display consistent types but varying behaviour, this varying behaviour can often be achieved through subtyping instead of through complex If and Case structures.

Therefore, where a program uses extensive conditional evaluation, consider whether it is not possible to restructure it to take advantage of interface inheritance. This may necessitate consciously structuring inheritance hierarchies to provide the subtyping needed for interface inheritance. This allows the programmer to code in terms of an interface instead of specific implementations.

The benefits of doing this are that the responsibility for taking the appropriate action is now assigned to a particular object, reducing coupling and so making the system more flexible and more amenable to future modification.

Summary of inheritance

Inheritance is often described as an IsA relationship: a subclass IsA superclass. It is also described as a generalisation-specialisation relationship: the superclass embodies all the common, general characteristics of its descendants; the subclass adds all those features that make it special to those it inherits from its ancestor(s). So a subclass is all that the superclass is plus more.

This leads to the situation that:

  • A subclass is an enriched version of its ancestors, and
  • A superclass has fewer capabilities than its descendants.
One result of inheritance is the concept of substitution (which, with dynamic binding, gives polymorphism): since a subclass has all the functionality of its ancestors, an instance of a subclass can substitute for an instance of any of its ancestors.

A subclass cannot reduce the visibility of any inherited attribute or method. If the data field FData is public in the superclass, the subclass cannot redefine it as private. However a subclass can increase visibility. So if FData is private in the superclass, the subclass can declare it to be public within the subclass. This latter process is referred to as exposing the superclass’s data or method.

There are two types of inheritance which are implemented similarly. In some ways this is very convenient, but it is also confusing since they are implemented in similar ways and little distinction is made between the two.

The more common kind of inheritance is implementation or class inheritance, also referred to as subclassing. For Gamma et al (1995, p17), class inheritance defines one object’s implementation in terms of another object’s implementation. It is a mechanism for code and representation sharing that supports the re-use of data and behaviour.

The second kind is interface or type inheritance, also referred to as subtyping. For Gamma et al (1995, p17), class inheritance describes when one object can be used in place of another. This is possible since the child can substitute for the parent. Provided the programming language has dynamic binding this allows polymorphic operations.

Class inheritance is generally over-used, giving rise to large, inflexible classes that are error-prone and difficult to maintain and extend, so undermining the benefits of OO. By contrast, interface inheritance is generally under-used with complex, error-prone and inflexible conditional structures being used instead. This too undermines the benefits of OO.

Much of this comes about because inheritance is a static relationship created by a completely rigid structure, making future maintenance potentially difficult should the relationships need to change and evolve.

Part 7 Download

There are a dozen of homework problems included in the Part's PDF. Make sure you solve all of them.

This is the end of Part 7 of the Delphi OOP Course.

Forward to Part 8 / Chapter 17: Indirection.

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

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

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Using and Abusing Inheritance - Delphi OOP Part 7 - Chapter 16

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

All rights reserved.