Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement multiple inheritance in delphi?

I'm doing a full rewrite of an old library, and I'm not sure how to handle this situation (for the sake of being understood, all hail the bike analogy):

I have the following classes:

  • TBike - the bike itself
  • TBikeWheel - one of the bike's wheel
  • TBikeWheelFront and TBikeWheelBack, both inherits from TBikeWheel and then implements the specific stuff they need on top of it

This is pretty straightforward, but now I decide to create multiple kind of bikes, each bikes having it's own kinds of wheel - they do the same stuff as a regular front/back wheels, plus the specific for that bike.

  • TBikeXYZ - inherits from TBike
  • TBikeWheelXYZ - inherits from TBikeWheel

And here is my problem: TBikeWheelFrontXYZ should inherit from TBikeWheelXYZ (to get the specific methods of an XYZ wheel), but it should also inherit from TBikeWheelFront (to get the specific methods of a front wheel).

My question here is, how can I implement that in a way that doesn't:

  1. feel like a hack
  2. force me to rewrite the same code several time
like image 723
Lepidosteus Avatar asked Aug 14 '09 05:08

Lepidosteus


1 Answers

Delphi does not support Multiple Inheritance. But classes can support / implement multiple interfaces and you can delegate interface implementation, so you can kinda simulate multiple inheritence.

like image 113
Tim Jarvis Avatar answered Oct 16 '22 07:10

Tim Jarvis